我想使用 AJAX 来确定表单的值是否可以接受(这不是表单验证)。阿贾克斯result
将确定是否提交表格。
下面,您将看到我在提交表单时执行 AJAX 调用,并且根据返回的内容(可接受的空白或不可接受的错误消息),我想return true;
or return false;
the $("form").submit
.
我怀疑我的麻烦出在 AJAX 上success:
。请帮我获取result
脱离 AJAX 调用,这样我就可以做类似的事情if (result == "") { return true; } else { return false; }
.
WORKING:
$("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
});