我有一個表單,當提交時,將返回一個成功的消息和一個新的表單與一個新的ID。例如,您輸入一個郵政編碼來計算到某個區域的距離,然後一旦該表單被提交,就會出現一個更改框。JQuery不處理Ajax表單提交
$("#PostcodeForm").submit(function (e) {
e.preventDefault();
$('#PostcodeError').text(" ");
$.ajax({
type: "POST",
url: "/Scripts/Scripts.php",
data: {Type:"SavePostcode",Postcode:$('#Postcode').val(),Slug:"<?php if(isset($_GET['ID'])){echo $_GET['ID'];}else{echo "None";}?>"},
success: function(response) {
if(response=="Error"){
$('#PostcodeError').text("That postcode could not be found. Please use the entire postcode and try again.");
}else{
$('#DistanceContent').slideUp("400", function() {
$('#DistanceContent').html(response).slideDown();
});
}
}
});
return false();
});
$("#PostcodeChangeForm").submit(function (e) {
e.preventDefault();
$.ajax({
type: "POST",
url: "/Scripts/Scripts.php",
data: {Type:"ChangePostcode"},
success: function(response) {
$('#DistanceContent').slideUp("400", function() {
$(this).html("Enter your postcode to see how far away this unit is!<br/><form method=\"post\" id=\"PostcodeForm\">Postcode: <input type=\"text\" name=\"Postcode\" id=\"Postcode\" maxlength=\"8\" /><input type=\"submit\" name=\"PostcodeSubmit\" id=\"PostcodeSubmit\" value=\"Calculate!\" /></form><span id=\"PostcodeError\"></span>").slideDown();
});
}
});
return false();
});
提交的第一份中的反應是:
"6 miles, as the crow flies, from POSTCODE.<br/><form method=\"post\" id=\"PostcodeChangeForm\"><input type=\"submit\" name=\"PostcodeChange\" id=\"PostcodeChange\" value=\"Change the Postcode!\" /></form>
我原來的HTML代碼:
<div class="Widget DistanceWidget">
<span class="Title">Distance:</span>
<span class="Content" id="DistanceContent">
Enter your postcode to see how far away this unit is!<br/>
<form method="post" id="PostcodeForm">
Postcode: <input type="text" name="Postcode" id="Postcode" maxlength="8" />
<input type="submit" name="PostcodeSubmit" id="PostcodeSubmit" value="Calculate!" />
</form>
<span id="PostcodeError"></span>
</span>
</div>
請告訴我這個'返回false();'??刪除它,你已經防止Windows默認行爲調用'e.preventDefault();' –
什麼問題?你有什麼跡象表明有什麼不對?如果我想*猜測*我高度懷疑你從未將'submit'處理程序綁定到第二個表單。它看起來像是在頁面加載時試圖綁定一次處理程序,但該元素在用戶交互之後才存在。 – David
'return false()'是防止事件發生的最有效方法嗎?預防一切。 – dfsq