Jquery 验证器动态参数
有没有办法将动态参数传递到自定义 jquery 验证方法中?具体来说,我正在寻找一种方法来比较 2 个控件,并希望将一个控件传递给另一个控件的验证方法进行比较。
这是我目前所拥有的:
//Add method
$.validator.addMethod("lowerInt", function(value, element, params) {
alert('inside method');
if (isNaN(parseInt(params)))
return true;
else
return parseInt(value) < parseInt(params);
}, $.validator.format("Please enter a number smaller than {0}"));
//Set validation
$("#form1").validate({
rules: {
lowInt: {
required: true,
integer: true,
lowerInt: 8 //I would like to pass a dynamic value here
}
}
});
如果我按照上面的方式运行它,它就会按预期工作。如果我将传递给 lowerInt 的 8 更改为 $('#highInt').val(),则似乎只为 lowerInt 函数设置一次值,并且永远不会更新它。我意识到我可以通过调用 $('#highInt').val() 来获取方法内部的值,但如果可能的话,我想传递该值。
Is there a way to pass dynamic parameters into a custom jquery validate method? Specifically, I was looking for a way to compare 2 controls and would like to pass one to the other's validate method for comparison.
Here is what I currently have:
//Add method
$.validator.addMethod("lowerInt", function(value, element, params) {
alert('inside method');
if (isNaN(parseInt(params)))
return true;
else
return parseInt(value) < parseInt(params);
}, $.validator.format("Please enter a number smaller than {0}"));
//Set validation
$("#form1").validate({
rules: {
lowInt: {
required: true,
integer: true,
lowerInt: 8 //I would like to pass a dynamic value here
}
}
});
If I run it as above, it works as it should. If I change the 8 passed in to lowerInt to $('#highInt').val(), it seems to set the value for the lowerInt function only once and never update it. I realize that I can get the value inside of the method by calling $('#highInt').val(), but I would like to pass the value instead if at all possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会传入一个选择器,如下所示:
此版本将采用数字或选择器,使其尽可能灵活。
I would pass in a selector, like this:
This version will take either a number or a selector, making it as flexible as possible.
将参数设为函数。
然后你在验证方法中调用该函数,如下所示
Make the parameter a function.
Then you call the function in the validate method something like