Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JQuery根据选择框验证文本字段_Jquery_Validation_Jquery Validate - Fatal编程技术网

JQuery根据选择框验证文本字段

JQuery根据选择框验证文本字段,jquery,validation,jquery-validate,Jquery,Validation,Jquery Validate,我正在尝试使用JQuery验证插件验证输入字段。 数值范围[range()]取决于选择框的值。 例如,当选择框为“A”时,范围应在1到10之间。但是当它的“B”值在5到10之间时,以此类推。我确实尝试过读取选择框的值,并调用函数验证输入,传递最小值 这是可行的,但是当你选择“A”并且你认为它应该是“B”时,它仍然验证为“A”。 我在考虑“依赖”,但我认为它只适用于“必需” $("#theSelectID").change(function () { var theValueOfS

我正在尝试使用JQuery验证插件验证输入字段。 数值范围[range()]取决于选择框的值。 例如,当选择框为“A”时,范围应在1到10之间。但是当它的“B”值在5到10之间时,以此类推。我确实尝试过读取选择框的值,并调用函数验证输入,传递最小值

这是可行的,但是当你选择“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."
                }
            }
         });
    }

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

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

感谢这项工作,它需要一些修改,以适应我的项目,但它的工作。