:空白选择器不工作复选框jQuery Validate()

:blank selector not working on checkbox jQuery Validate()

本文关键字:jQuery Validate 复选框 工作 空白 选择器      更新时间:2023-09-26

嗨,我目前有2个字段,其中一个或另一个需要被选中,这些最初使用2个文本类型输入,现在其中一个需要从文本输入更改为复选框。

逻辑:如果文本框有值,则不需要复选框;如果复选框被选中,则不需要输入文本框。

我最初使用:blank选择器来确定验证,它正在工作,将其更改为复选框,它坏了,复选框是否有不同的规则,我从validate()文档中缺失?

我使用以下版本:

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.13.1/jquery.validate.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.13.1/additional-methods.js"></script>
jQuery:

        //text 1
        'textbox': {
            required: "#certificate:blank",
            minlength: 8
        },
        //checkbox 1
        certificate: {
            required: "#textbox:blank"
        },
HTML:

<input type="text" maxlength="9" class=" form-textbox" id="textbox" name="textbox">
<input type="checkbox" class=" form-checkbox" id="certificate" name="certificate">

您需要从

更新代码
certificate: {
    required: "#textbox:blank"
},

// for checked
certificate: {
    required: "#textbox:checked"
},
// for unchecked
certificate: {
    required: function(element) {
        return !$("#textbox").is(":checked")
    }
},

参考- http://jqueryvalidation.org/category/methods/#example:-makes-details-required-only-if-#other-is-checked