2
嘗試使用jquery mobile做一個簡單的web應用程序。我把一個簡單的登錄表單放在一起,並顯示ajax結果。問題是我的ajax沒有得到結果,即使我警惕地看到URL是否有效。有什麼特別的我需要使用jQuery的手機嗎?Jquery Mobile - 獲取ajax結果問題
任何想法/答案非常感謝!
下面是HTML代碼:
<div data-role="page" id="login" data-theme="a" data-add-back-btn="true">
<div data-role="header">
<h2>Log in</h2>
</div>
<div data-role="content" data-theme="a" data-add-back-btn="true">
<form action="mobilelogin.php" method="post">
Email: <input type="text" name="email" id="useremail">
Password: <input type="password" name="password" id="userpassword">
<input type="submit" value="Enter">
</form>
<div id="loginresult"></div>
</div>
<div data-role="footer"><h4>Chris Sherwood Design 2012</h4></div>
</div>
js文件:
$(document).ready(function() {
$('form').submit(function() {
var emailchecker = /^([\w-\.][email protected]([\w-]+\.)+[\w-]{2,4})?$/;
var email = $("#useremail").val();
var userpassword = $("#userpassword").val();
var checklength = userpassword.length;
if(email == ""){
alert('Please fill in email field');
return false;
}else if(userpassword == ""){
alert('Please fill in password field');
return false;
}else if((!emailchecker.test(email))){
alert('Please use a valid email');
return false;
}else if(checklength < 6 || checklength > 6){
alert('Password must be six characters');
return false;
}else{
var datastring = $(this).serialize();
alert(datastring);
return false;
$.ajax({
type: "POST",
url: "mobilelogin.php",
data: datastring,
success: function(data){
$("#loginresult").html(data);
}
});
}
});
PHP
<?php
echo 'nothing special here...';
?>
如果我刪除返回false頁面重定向到'mobilelogin.php'並給出一個未定義的。感謝您的答案更多的想法? – ChrisSherwood 2012-03-08 13:07:41
您仍然想要阻止默認提交操作 - 將其移回或返回false,或使用事件對象執行此操作。我會在你的答案中說清楚。 – Tjkoopa 2012-03-08 13:21:09
答案 - $阿賈克斯({ 類型: 「POST」, 網址: 「mobilelogin.php」, 數據:datastring, 成功:功能(數據){ $( 「#loginresult」)HTML(數據) ; } }); 返回false; 'return false;'需要在ajax調用下!總是在這個遊戲中學習! – ChrisSherwood 2012-03-08 13:23:29