【问题标题】:Ignore option in jquery validation doesn't workjquery验证中的忽略选项不起作用
【发布时间】:2021-01-31 16:19:06
【问题描述】:

我想验证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: '' 设置为替换所有忽略选项,忽略选项也不起作用,但它仍然无法隐藏隐藏字段

【问题讨论】:

  • 它应该被反转,.fileInput:not(:hidden),这意味着它应该监视所有不是 :hidden 的“.fileInput”
  • 我希望该文件输入得到验证,

标签: jquery validation jquery-plugins unobtrusive-validation


【解决方案1】:

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

所以我在循环验证之前添加了这一行并且它起作用了

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

【讨论】:

    猜你喜欢
    • 2015-02-15
    • 1970-01-01
    • 1970-01-01
    • 2011-10-08
    • 2018-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多