微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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)";

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。