本文介绍了jQuery验证-获取invalidHandler中错误字段的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在页面上使用jQuery验证.在调用invalidHandler的过程中,我希望能够访问所有未通过验证的表单元素的列表.

I'm using jQuery Validation on a page. During the call to the invalidHandler I would like to be able to access a list of all the form elements that failed validation.

此函数作为选项之一传递给jQuery.validate()方法...

This function is being passed as one of the options to the jQuery.validate() method...

invalidHandler: function (form) {
    var validator = $("#AddEditFinancialInstitutionForm").validate();
    validator.showErrors();
    console.log(validator);
}

我试图在生成的验证器对象中的某处找到此信息,但是似乎找不到.我还有另一种方式可以访问此信息吗?

I'm trying to find this information somewhere in the resulting validator object, but I can't seem to find it. Is there another way I can access this information?

谢谢

推荐答案

invalidHandler中,传递了两个参数,分别是jQuery.Eventvalidator对象.您无需在invalidHandler中调用validate即可获取validate对象.此外,验证器对象具有名为errorListerrorMap的属性,其中包含您要查找的信息.

In the invalidHandler, you are passed two arguments, a jQuery.Event and the validator object. You don't need to call validate within your invalidHandler to get the validate object. Further, the validator object has a properties called errorList and errorMap, which contain the information you are looking for.

invalidHandler: function(e,validator) {
    //validator.errorList contains an array of objects, where each object has properties "element" and "message".  element is the actual HTML Input.
    for (var i=0;i<validator.errorList.length;i++){
        console.log(validator.errorList[i]);
    }

    //validator.errorMap is an object mapping input names -> error messages
    for (var i in validator.errorMap) {
      console.log(i, ":", validator.errorMap[i]);
    }
}

这篇关于jQuery验证-获取invalidHandler中错误字段的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 13:29