Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.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
Javascript 将验证方法添加到";jquery验证插件";_Javascript_Jquery - Fatal编程技术网

Javascript 将验证方法添加到";jquery验证插件";

Javascript 将验证方法添加到";jquery验证插件";,javascript,jquery,Javascript,Jquery,你好,我正在使用jquery验证插件验证我的表单。 我想在验证中再添加一条规则,但无法使其生效。 输入值应小于或等于先前的元素值。 有可能吗 这是密码- <input type='text' value='<?php echo $count; ?>' class='input_in_stock' readonly="readonly" /> <input type='text' name='amount[]' id='<?php echo rand(0,99)

你好,我正在使用jquery验证插件验证我的表单。 我想在验证中再添加一条规则,但无法使其生效。 输入值应小于或等于先前的元素值。

有可能吗

这是密码-

<input type='text' value='<?php echo $count; ?>' class='input_in_stock' readonly="readonly" />
<input type='text' name='amount[]' id='<?php echo rand(0,99).$id; ?>' size='1' value='<?php echo $count; ?>' class='required input_count' />

$(document).ready(function(){

    $.validator.addMethod('positiveint', function (value) { 
        return (value > 0) && (value != 0) && (value == parseInt(value, 10));
    }, 'Enter a positive integer value.');

    $.validator.addMethod('notmorethan', function (value) { 
        return (value <= $(this).prev().val());
    }, 'abc.');




    $("#cartform").validate({
        rules: {
            "amount[]": {
                required: true,
                positiveint: true,
                notmorethan: true
            }
        },
        errorPlacement: function(error, element) {
            error.appendTo(element.next());
        }
    });

});
看看这把小提琴是否有用-。我不确定您使用的自定义名称,如
amount[]
,但我想这种方法应该非常有效

自定义验证方法只是函数,如果在函数体中使用
this
,则它将引用函数的所有者。现在假设您希望从输入字段中获取以前的有效值,只要整个表单有效,就可以将其存储在DOM中的某个位置,比如说,以前存储的值被称为
previousAmountVal
。更改字段中的值时,在函数
中,您可以使用
value
参数访问当前值,使用
previousAmountVal
访问以前存储的值和以前的值,从而确定当前值是否有效。此外,如果执行此操作,请记住在表单有效时始终更新
previousAmountVal

找到了解决方案

$.validator.addMethod('notmorethan', function (value, element) { 
    return (value <= $(element).prev().val() );
}, 'abc');
$.validator.addMethod('notmorethan', function (value, element) { 
    return (value <= $(element).prev().val() );
}, 'abc');
$.validator.addMethod('notmorether',函数(值,元素){

return(value)我自己已经找到了解决方案,但仍然需要thx帮助!请将您的解决方案作为答案发布并接受它。