覆盖jQuery验证插件中的一个函数 [英] overriding a function within the jquery validation plugin

查看:95
本文介绍了覆盖jQuery验证插件中的一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 jquery表单验证插件来验证长时间形成。如果用户提交包含多个未验证的字段的表单,则光标将返回到未通过的最后一个字段。相反,我希望将光标返回到未通过验证的表单中的第一个字段。而不是修改插件,我想重写focusInvalid,它嵌套在prototype:{...}中:

  focusInvalid:function(){
if(this.settings.focusInvalid){
try {
$(this.findLastActive()|| this.errorList.length&&& this) errorList [0] .element || [])。filter(:visible)。focus();
} catch(e){
//忽略IE关注隐藏元素时抛出错误
}
}
},

我试过在下面添加我自己的focusInvalid版本,但是我一直无法让它工作:

  $(form#user-register)。validate({。。。}); 

任何指针?

解决方案

您可以使用 focusInvalid 选项禁用第一个无效输入的选择。然后可以使用 invalidHandler 选项设置一个自定义函数,用于选择表单中的第一个元素。 invalidHandler 选项是插件在提交无效表单时调用的函数。



下面的示例:

  $(form#user-register)。validate({
invalidHandler:function(form,validator){
$(validator.invalidElements()[0])。focus();
},
focusInvalid:false
});


I am using the jquery form validation plugin to validate a long form. If a user submits the form with multiple fields that fail to validate, the cursor returns to the last field that did not pass. Instead, I want the cursor to be returned to the first field in the form that failed validation. Rather than modifying the plugin, I would like to override focusInvalid, which is nested inside "prototype: {...}":

focusInvalid: function() {
    if( this.settings.focusInvalid ) {
        try {
            $(this.findLastActive() || this.errorList.length && this.errorList[0].element || []).filter(":visible").focus();
        } catch(e) {
            // ignore IE throwing errors when focusing hidden elements
        }
    }
},

I've tried adding my own version of focusInvalid within the following, but I haven't been able to get that to work:

$("form#user-register").validate({. . .});

Any pointers?

解决方案

You can disable the selection of the first invalid input with the focusInvalid option. You can then use the invalidHandler option to set a custom function that selects the first element in your form. The invalidHandler option is the function that the plugin calls when an invalid form is submitted.

Example below:

$("form#user-register").validate({
     invalidHandler: function(form, validator){
         $(validator.invalidElements()[0]).focus();
     },
     focusInvalid:false
});

这篇关于覆盖jQuery验证插件中的一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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