【问题标题】:JQuery Validating text field depending on select boxJQuery验证文本字段取决于选择框
【发布时间】:2010-03-05 11:46:48
【问题描述】:

我正在尝试使用 JQuery 验证插件来验证输入字段。 数值范围 [range()] 取决于选择框的值。 For example when the select box is "A" the range should be between 1 and 10. But when its "B" it should be between 5 and 10 and so on.我确实尝试过读取选择框的值并调用一个函数来验证输入,传递最小值。

这可行,但是当您选择“A”并且您认为它应该是“B”时,它仍然验证为“A”。 我在考虑“取决于”,但我认为它只适用于“必需”。

$("#theSelectID").change(function () {
        var theValueOfSelected = $('#LeverancierID :selected').text();
        switch(theValueOfSelected){
        case "A":
            minval(1, "Null");
            break;
        case "B":
            minval(5, "PRCC");
            break;
            }
            function minval(theVal, theLev){
        $("#AddInkoop").validate({  
            rules: {
                Aantal: {
                    required: true,
                    range: [theVal, 999]
                }
            },
            messages: {
                Aantal:{
                    required:"required",
                    range: "give a number between "+ theVal +" and 999."
                }
            }
         });
    }

【问题讨论】:

    标签: jquery jquery-validate


    【解决方案1】:

    我认为你是在倒退。不要在每次选择框更改时更改验证设置,而是设置验证规则,以便使用反映选择框当前值的函数计算最小值。请注意,您可能需要更新一些描述性文本,以指示选择更改时可接受的范围。

     $("#AddInkoop").validate({  
            rules: {
                Aantal: {
                    required: true,
                    range: function() { return [ $('#LeverancierID').val(), 999 ]; }
                }
            },
            messages: {
                Aantal:{
                    required:"required",
                    range: "the value is outside the acceptable range"
                }
            }
    });
    

    【讨论】:

    • 感谢这个工作,它需要一些修改以适应我的项目,但它工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-09
    相关资源
    最近更新 更多