我想使用AJAX来确定表单的值对我来说是否可以接受(这不是表单验证)。AJAX result将确定是否提交表单。
下面,你会看到,当表单提交并根据返回什么我执行一个AJAX调用(无论是空白这是可以接受的,或者这是不能接受的错误信息),我想return
true;还是return false;在$("form").submit。
我怀疑我的麻烦在于AJAX的success:。请帮助我摆脱resultAJAX呼叫,以便我可以执行类似的操作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
});