如何查看有错误元素的 jQuery 验证列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11952570/
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-26 11:07:41  来源:igfitidea点击:

How to see jQuery validation list of elements with errors

jqueryasp.net-mvc-3validationjquery-validate

提问by Dragos Durlut

Sometimes, the form won't submit because jQuery has some invalid elements that will not show up in an error message.

有时,表单不会提交,因为 jQuery 有一些无效元素不会显示在错误消息中。

How can we see these errors in order to debug more easily ?

我们如何才能看到这些错误以便更轻松地进行调试?

回答by Dragos Durlut

var validator = $("form").validate()
validator.errorList

will show the array of errors that are holding back the form from submiting.

将显示阻止表单提交的一系列错误。

回答by RickL

This works for me to get a list of validation errors (ids of error inputs and associated error message):

这对我有用以获取验证错误列表(错误输入的 ID 和相关的错误消息):

    if ($('#form').valid()) {

        console.log('FORM VALID');

    } else {

        console.log('FORM INVALID');

        var validator = $('#form').validate();

        $.each(validator.errorMap, function (index, value) {

            console.log('Id: ' + index + ' Message: ' + value);

        });

    }

回答by KimboSlice

 var val = $("#form".validate());
 console.log("error list", val);

for those who dont get errorList working that way, just go to console and click on the errorList

对于那些没有以这种方式工作的 errorList 的人,只需转到控制台并单击 errorList

回答by zeusstl

errorList only seems to work exactly right for me after running a call with valid() in it.

errorList 似乎只在运行带有 valid() 的调用后对我来说完全正确。

var validator = jQuery('#form1').validate();
if(jQuery('#form1').valid()){
    var submitErrorsList = new Object();
    for (var i=0;i<validator.errorList.length;i++){
        submitErrorsList[validator.errorList[i].element.name] = validator.errorList[i].message;
    }
}
console.log("Submit Errors", submitErrorsList);