我正在使用一本書來學習JavaScript,並且我在最後一章中講述了jquery和ajax。書中的例子不起作用。我甚至從一個網站下載了文件,我發現它已經由作者上傳並進行了檢查。當我嘗試提交表單時,它只會彈出提醒。下面是代碼:jQuery的書中的Ajax問題
<!DOCTYPE html>
<html>
<head>
<title>Ajax Form Submission</title>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(document).ready(function(){
function checkFields(){
return ($("#name").attr("value") && $("#email").attr("value"));
}
$("#form1").submit(function(){
if(checkFields()){
$.post(
'test.php', $("#form1").serialize(),
function(data){
$("#div1").html(data);
}
);
}
else alert("Please fill in name and email fields!");
return false;
});
});
</script>
</head>
<body>
<form id="form1">
Name<input type="text" name="name" id="name"><br />
Email<input type="text" name="email" id="email"><br />
<input type="submit" name="submit" id="submit" value="Submit Form">
</form>
<div id="div1"></div>
</body>
</html>
這裏是它呼籲PHP:
<?php
echo "Name: ". $_Request['name'] . "<br />Email:" . $_Request['email'];
?>
什麼不行? – 2013-03-26 02:09:30
剛剛編輯了這個問題,當試圖提交時,無論框中的內容如何,警報都會彈出,因此它永遠不會實際進入腳本的$ .post部分 – Michael 2013-03-26 02:12:30
您可以發佈您的html代碼嗎? – 2013-03-26 02:13:35