我有一个包含多个表单的页面。每个表单都有自己的Save按钮,它使用.ajax()验证并显示错误消息(不是普通的post方法)。我还在表单之外有一个由jQuery click()处理程序捕获的Next按钮。此函数遍历每个表单并为每个表单调用$(formname).submit()。
最终,我需要知道各个表单是否通过了验证,以确定是否应该进行重定向。我在submit函数的validate check的else语句中添加了return false;
,但我看不到如何检索它。
让alert($(this).submit());
只返回对象而不是返回值。如果不重新编码每个表单的整个$ .ajax邮政编码,无论如何我都能找出提交是否失败?
//next button click
$('#savenext').click(function () {
var finalsuccess;
$('.record_form').each(function (index) {
alert($(this).submit()); //I want this to be true or false
});
//forms each have their own submit handler
$(formname).submit(function (e) {
e.preventDefault();
var form = $(this);
if($(form).valid()) { //validate the form, return false if it is not valid.
//Post the form to the action if it passes
$.ajax({
url: form.attr('action'),
type: "POST",
dataType: "text json",
data: form.serialize(),
success: function (response) {
//does some success messages here
},
});
}
else {
return false;
}
});
由于
答案 0 :(得分:3)
如果有人需要知道,我发现无法让提交返回成功值。最后我将ajax帖子更改为async:false,将其放在一个函数中,该函数返回由ajax post的成功或错误事件设置的布尔值。由于服务器调用的连续性,速度并不快,但我会添加一个加载对话框来帮助解决这个问题。
答案 1 :(得分:0)
我在寻找问题的解决方案时发现了这个问题,但找不到任何有效的方法。我最终不得不“钩住” jQuery提交功能,下面的代码以防有人也有同样的问题。
// override jQuery.submit() so that it returns true/false rather than a jQuery object
var original_submit = jQuery.fn.submit;
jQuery.fn.submit = function() {
// we are only interested in the .submit() trigger calls
if (arguments.length == 0)
{
var ret = $(this).triggerHandler("submit");
// assume true unless we specifically returned false
return ret === false ? false : true;
}
// fallback to original function
return original_submit.apply(this, arguments);
};