防止表单在jQuery Validate插件的submitHandler函数中提交 [英] Preventing a form from submitting in jQuery Validate plugin's submitHandler function

查看:349
本文介绍了防止表单在jQuery Validate插件的submitHandler函数中提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 http://docs.jquery.com/Plugins上使用jQuery和validate插件/验证/验证

我想阻止表单在通过ajax完成验证和提交过程后提交。
我有以下代码:

I want to prevent the form from submitting after its validation and submission processes done via ajax. I have the following code:

$("#myform").validate({
   rules: {...},
   submitHandler: function(form) { 
      alert("Do some stuff...");
      //submit via ajax
      return false;  //This doesn't prevent the form from submitting.
   }
});

然而,问题在于 submitHandler 即使我在处理函数的最后一行有一个 return false; 语句,也会提交表单。我想要阻止默认行为并停止提交表单,因为我已经通过ajax提交了。

However, the problem with this is that the submitHandler submits the form even though I have a return false; statement at the last line of the handling function. I want prevent the default behaviour and to stop the form from submitting because I have already submitted via ajax.

在通过 submitHandler 函数中的ajax代码后,如何停止提交代码?

How should I stop the code from submitting after going through the ajax codes in the submitHandler function?

推荐答案

我是这样做的,它完全符合你的工作方式:

I do it like this and it works exactly how you want it to work:

$("#myform").submit(function(e) {
    e.preventDefault();
}).validate({
    rules: {...},
    submitHandler: function(form) { 
        alert("Do some stuff...");
        //submit via ajax
        return false;  //This doesn't prevent the form from submitting.
    }
});

这篇关于防止表单在jQuery Validate插件的submitHandler函数中提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆