我想使用 AJAX 来确定我是否可以接受表单的值(这不是表单验证)。AJAXresult
将确定表单是否提交。
下面,您将看到我在提交表单时执行 AJAX 调用,具体取决于返回的内容(可接受的空白或不可接受的错误消息) ,我希望return true;
或return false;
.$("form").submit
我怀疑我的麻烦在于 AJAX 的success:
. 请帮助我摆脱result
AJAX 调用,以便我可以执行类似if (result == "") { return true; } else { return false; }
.
在职的:
$("form").submit(function(e) {
e.preventDefault();
var form = this;
var tray = $('select[name=tray_id]').val();
$.ajax({
type: "POST",
url: "modules/reserve-check.php",
data: {tray_id: tray},
cache: false
}).done(function(result) {
if (result == "")
form.submit();
else
alert(result);
}).fail(function() {
alert('ERROR');
});
});
原来的:
$("form").submit(function() {
var tray = $('select[name=tray_id]').val();
$.ajax({
type: "POST",
url: "modules/reserve-check.php",
data: {tray_id: tray},
cache: false,
success: function(result) {
alert(result);
},
error: function(result) {
alert(result); //This works as expected (blank if acceptable and error msg if not acceptable)
}
});
/*
if (result == "")
return true;
else
return false;
*/
return false; //this is here for debugging, just to stop the form submission
});