如何将 jquery.Validate 与 jquery.multiselect 下拉菜单一起使用?

发布于 2024-12-21 23:29:52 字数 2359 浏览 0 评论 0原文

所以情况是这样的:尝试使用 jquery.multiselect< 添加下拉框当前使用 jquery.validate 插件的表单上的 /a> 插件有其他字段(文本输入字段、具有浮点值范围的单个文本输入)当前均已正确验证。

当我尝试添加验证规则时,我根本无法让 jquery.validate 来验证我的多选下拉列表。以下是我的代码片段(全部假设已加载所需插件 - 请参阅下文了解使用的版本):

HTML:

<form action="some/action" id="myForm" method="POST">
    Input 1: <input type="text" value="" name="input1" maxlength="200" id="input1"><br/>
    Input 2: <input type="text" value="" name="input2" maxlength="100" id="input2"><br/>
    Input 3: <input type="text" value="" name="input3" maxlength="50" id="input3"><br/>
    Select: <select class="someSelect" name="mySelect" id="mySelect" multiple="multiple">
        <option value="some_val1">Some Value</option>
        <option value="some_val2">Some Other Value</option>
    </select>
    <input type="submit" value="Submit" />
</form>

Javascript:

$(document).ready(function() {
    $('#mySelect').multiselect({
        noneSelectedText: 'Select Something (required)',
        selectedList: 3,
        classes: 'my-select'
    });

    $.validator.addMethod("needsSelection", function(value, element) {
        return $(element).multiselect("getChecked").length > 0;
    });

    $.validator.addMethod("isPercent", function(value, element) {
        return parseFloat(value) >= 0 && parseFloat(value) <= 100;
    });

    $.validator.messages.needsSelection = 'You gotta pick something.';
    $.validator.messages.isPercent = 'Must be between 0% and 100%';

    $('#myForm').validate({
        rules: {
            mySelect: "required needsSelection",
            input1: "required isPercent",
            input2: "required",
            input3: "required"
        },
        errorClass: 'invalid'
    });
});

版本 如果版本之间存在明显/已知的兼容性问题,那么如果可以解决问题,我可能会进行升级,但我已经出于我的目的使用最新版本进行了测试,但它似乎没有解决我的问题。

jQuery:1.4.4

jquery.validate:1.9.0

jquery.multiselect:1.8

并且,一如既往,我可以尽可能提供更多信息/需要。

So the situation is this: attempting to add a dropdown box using the jquery.multiselect plugin on a form that current uses the jquery.validate plugin that has other fields (text input fields, single text input that has a float value range) that all currently validate correctly.

When I attempt to add validation rules I simply cannot get jquery.validate to validate my multiselect dropdown whatsoever. Here are snippets of my code (all assumes that required plugins are loaded - see below for versions used):

The HTML:

<form action="some/action" id="myForm" method="POST">
    Input 1: <input type="text" value="" name="input1" maxlength="200" id="input1"><br/>
    Input 2: <input type="text" value="" name="input2" maxlength="100" id="input2"><br/>
    Input 3: <input type="text" value="" name="input3" maxlength="50" id="input3"><br/>
    Select: <select class="someSelect" name="mySelect" id="mySelect" multiple="multiple">
        <option value="some_val1">Some Value</option>
        <option value="some_val2">Some Other Value</option>
    </select>
    <input type="submit" value="Submit" />
</form>

The Javascript:

$(document).ready(function() {
    $('#mySelect').multiselect({
        noneSelectedText: 'Select Something (required)',
        selectedList: 3,
        classes: 'my-select'
    });

    $.validator.addMethod("needsSelection", function(value, element) {
        return $(element).multiselect("getChecked").length > 0;
    });

    $.validator.addMethod("isPercent", function(value, element) {
        return parseFloat(value) >= 0 && parseFloat(value) <= 100;
    });

    $.validator.messages.needsSelection = 'You gotta pick something.';
    $.validator.messages.isPercent = 'Must be between 0% and 100%';

    $('#myForm').validate({
        rules: {
            mySelect: "required needsSelection",
            input1: "required isPercent",
            input2: "required",
            input3: "required"
        },
        errorClass: 'invalid'
    });
});

Versions
If there's an explicit/known issue with compatibility between the versions, then I may upgrade if that solves the issue, but I've tested using the newest versions for my purposes and it did not seem to solve my issue.

jQuery: 1.4.4

jquery.validate: 1.9.0

jquery.multiselect: 1.8

And, as always, I can provide more information where possible/needed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你怎么这么可爱啊 2024-12-28 23:29:52

因此,看来我为多选设置的规则确实附加到了选择,但是,由于原始选择框是 :hidden by jquery.multiselectjquery.validate 默认情况下会忽略任何隐藏的输入。

解决方案(不是我自己的 - 同事发现的)是使用 jquery.validateignore 设置。无论使用 ignore 设置传入什么选择器,都会将其放入 jquery .not() 中,因此解决方案实际上是在这里使用双重否定:

$('#myForm').validate({
    rules: {
        mySelect: "required needsSelection",
        input1: "required isPercent",
        input2: "required",
        input3: "required"
    },
    ignore: ':hidden:not("#mySelect")', //Tells it to check the hidden select
    errorClass: 'invalid'
});

唷!

So it seems that the rules I was setting up for the multiselect were indeed being attached to the select, however, since the original select box is :hidden by jquery.multiselect, jquery.validate by default ignores any hidden inputs.

The solution (which is not of my own - a coworker found it) is to use the ignore settings for jquery.validate. Whatever selector is passed in with the ignore setting is put into a jquery .not(), so the solution is actually to use a double negative here:

$('#myForm').validate({
    rules: {
        mySelect: "required needsSelection",
        input1: "required isPercent",
        input2: "required",
        input3: "required"
    },
    ignore: ':hidden:not("#mySelect")', //Tells it to check the hidden select
    errorClass: 'invalid'
});

Phew!

谜泪 2024-12-28 23:29:52

在我看来,您的两个选择选项具有价值。

value="some_val1"

尝试在顶部添加一个值为空的选项并选择:

<option selected="" value="">Please Select Something</option>

Looks to me like your two select options carry a value.

value="some_val1"

Try adding an option at the top with a value of nothing and selected:

<option selected="" value="">Please Select Something</option>
把时间冻结 2024-12-28 23:29:52

另一种可能的解决方案是显式指定要验证的元素:

$("form").children("input, select, textarea").valid()

这也将验证隐藏的选择。

事实上,我刚刚遇到的情况没有其他方法,因为我想对多步骤表单进行部分验证:

 $("form").find("[data-stepIndex='1']").children("input, select, textarea").valid()

希望它对某人有帮助

Another possible solution is to explicitly specify the elements to validate:

$("form").children("input, select, textarea").valid()

That will validate the hidden select as well.

In fact, the situation I just encountered there was no other way since I wanted to validate by part for a multistep form:

 $("form").find("[data-stepIndex='1']").children("input, select, textarea").valid()

Hope it helps someone

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文