问题描述
我在语法上遇到麻烦,无法在输入字段验证中获得2个范围值.
I'm having trouble with the syntax to get 2 range values on an input field validation.
我的表单有2个选择字段和1个文本输入字段.
My form has 2 select fields and one text input field.
If select1 = 1 and select2 = A
,我希望文本字段上的范围值为1 to 120
.
If select1 = 1 and select2 = A
, I would like the range value on the text field to be 1 to 120
.
If select1 = 1 and select2 = B
,我希望文本字段上的范围值为121 to 500
.
If select1 = 1 and select2 = B
, I would like the range value on the text field to be 121 to 500
.
我的验证码:
PrepurchasePlotNumber: {
digits: true,
required: {
depends: function() {
return ($("input[name=PlotType]:checked").val() == "Prepurchase");
}
},
range: {
param: [1, 120],
depends: function() {
return ($("#PrepurchasePlotArea").val() == "1") && ($("#PrepurchasePlotRow").val() == "A");
}
},
range: {
param: [121, 500],
depends: function() {
return ($("#PrepurchasePlotArea").val() == "1") && ($("#PrepurchasePlotRow").val() == "B");
}
}
},
数字和所需工作正常.我无法正确测试两个范围方案的代码.一次只能工作一个.请有人能指出正确的方向.
Digits and required work fine.I can't get my code right to test for both range scenarios. Only one will work at any one time.Please can someone point me in the right direction.
推荐答案
因为您不能在同一字段上两次声明相同的规则.第二个可能会超越第一个.
Because you cannot have the same rule declared twice on the same field. The second will likely over-ride the first.
此外,使用depends
编写代码的方式是说,仅在满足条件时才需要range
规则.
Also, the way you've written it using the depends
, you're saying you only want the range
rule when the condition is met.
您只需根据条件使用函数来return
参数.
You simply need to use a function to return
the parameter depending on the conditional.
PrepurchasePlotNumber: {
digits: true,
range: function() {
if (($("#PrepurchasePlotArea").val() == "1") && ($("#PrepurchasePlotRow").val() == "A")) {
return [1, 120]
} else {
return [121, 500]
}
},
....
根据需要修改逻辑.
这篇关于jQuery验证-取决于值的相同规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!