jQuery Validate resetForm()不会重置onFocus验证 [英] jQuery Validate resetForm() doesn't reset the onFocus validation

查看:1090
本文介绍了jQuery Validate resetForm()不会重置onFocus验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用jQuery Validate插件进行客户端验证的表单,并通过AJAX提交.提交表单后,我希望它重置自己.我已经使用.resetForm()方法重置字段了,但是有效,但是问题是validate插件仍然会在关注字段时立即尝试验证字段,并在给新提交机会输入错误之前给出错误提示输入.

I have a form that is using jQuery Validate plugin for client side validation, and submits via AJAX. After the form is submitted, I would like it to reset itself. I have used the .resetForm() method to reset the fields, which works, but the problem is that the validate plugin still immediately tries to validate fields when they're focused, and gives an error before giving the new submission a chance to enter their input.

示例流程:

  • 最初的触发验证
  • 修正验证,通过AJAX提交表单
  • 调用.resetForm(),成功清除了字段
  • 尝试再次填写表格
  • 关注某个字段(在我的情况下,该字段具有电子邮件验证)
  • 在给您输入输入的机会之前,它会进行验证并说不正确.

通常,插件会在告诉您不正确之前让您输入您的输入.有什么方法可以真正地重置表单,而又不用关闭焦点验证就可以再次使焦点再次出现?

Typically the plugin will let your enter your input before telling you that it's incorrect. Is there any way to truely reset the form and not have the validation occur on focus again, without turning off on focus validation all together?

谢谢,这是一个清理/简化的代码示例!

Thanks, and here's a cleaned up/simplified code samples!

    // Validation
    $('#registerForm').validate({
        submitHandler: function(form) {

            $(form).ajaxSubmit({
                success: function(response) {

                    if( response == 'success' ) {
                        resetMyForm();
                    }
                    else {
                        alert('unsuccessful');
                    }

                }
            })
        }
    });

})




function resetMyForm() {

        $('#registerForm').resetForm();
        v.prepareForm();  // Tried adding this, no help
        v.hideErrors();  // Tried adding this, no help

}

推荐答案

您需要重置表单并运行Validation插件方法resetForm,如下所示:

You need to reset the form and run the Validation plugin method resetForm like this:

var v = $("form").validate({
    submitHandler: function(form) {
            //resetForm is a method on the validation object, not the form
            v.resetForm(); 
            form.reset();
    }
});

这似乎可以解决我在遵循工作流程时遇到的所有问题.看到以下内容: http://jsfiddle.net/nxXNz/27/

That seemed to take care of any issues I saw while following your workflow. See this: http://jsfiddle.net/nxXNz/27/

这篇关于jQuery Validate resetForm()不会重置onFocus验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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