jQuery验证中的忽略选项不起作用

问题描述

我想验证jquery中的一些隐藏元素

<label class="btn col-1" style="padding-right:0px;">
    <input id="Cert1" asp-for="Cert1.CertimageFile" onchange="SetimagePath('#Certimage1','#Cert1')" type="file" class="fileInput" accept=".jpg,.jpeg,.png" hidden>
    <img src="~/images/browse.png" width="23" />
    <small><span asp-validation-for="Cert1.CertimageFile" class="text-danger font-weight-bold"></span></small>
</label>


function validateForm() {
                // This function deals with validation of the form fields
                var x,y,i,valid = true;
                x = document.getElementsByClassName("tab");
                y = x[currentTab].querySelectorAll("input,select");
                
                // A loop that checks every input feld in the current tab:
                var form = $("#regForm");
                for (i = 0; i < y.length; i++) {
                    // If a field is not valid...
                    if (!form.validate({
                        ignore: ':hidden:not(.fileInput)'
                    }).element(y[i])) {
                        // add an "invalid" class to the field:
                        y[i].className += " invalid";
                        // and set the current valid status to false
                        valid = false;
                    }
                }
                
                return valid; // return the valid status
            }

但是即使我将ignore: ''设置为替换所有忽略选项,忽略选项也不起作用,它仍然无法使隐藏字段失效

解决方法

我发现Unobtrusive插件在加载文档时将validate()调用并将选项设置为默认值。一个验证器,然后将其缓存,并且随后进行的任何验证调用都将忽略新选项,并返回缓存的验证器

所以我在循环验证之前添加了这一行,它行得通了

$("#regForm").validate().settings.ignore = ":hidden:not(.fileInput)";