jQuery validate submitHandler 触发验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7581626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:01:02  来源:igfitidea点击:

jQuery validate submitHandler triggers validation

jqueryvalidation

提问by james

When using the jQuery submitHandler method, I'm posting the form data via ajax to the server. On success, another method is called that resizes a popup and notifies the user its all complete.

使用 jQuery submitHandler 方法时,我通过 ajax 将表单数据发布到服务器。成功后,将调用另一种方法来调整弹出窗口的大小并通知用户它已全部完成。

My problem is however, whilst posting the data, the form is getting validated again causing undesired effects.

然而,我的问题是,在发布数据时,表单再次得到验证,导致不良影响。

My understanding is submitHandler shouldn't trigger the validation again?

我的理解是 submitHandler 不应再次触发验证?

submitHandler: function(form) {
    console.log('test');
}

In this instance, 'test' is being logged to the console, but the validation is being run once again (in this case, a server side validity check on an email address field).

在这种情况下,'test' 被记录到控制台,但验证正在再次运行(在这种情况下,对电子邮件地址字段的服务器端有效性检查)。

I'm using a button type submit as the form element. Do I need to prevent defaults or something similar?

我使用按钮类型提交作为表单元素。我是否需要防止默认值或类似的东西?

回答by Aaron McIver

Your submitHandlershould exist within the validatefunction. In addition you should call form.submit()yourself which will remove the recursive calls that would otherwise exist.

submitHandler应该存在于validate函数中。此外,您应该调用form.submit()自己,这将删除否则会存在的递归调用。

 $("#myForm").validate({
      submitHandler: function (form) {
                     console.log('test');
                     form.submit();
                          }
                      });