[英]Jquery Validate Depends changing rule value of minimum
Javascript Java脚本
dollars: {
required: true,
number: true,
min:{
depends: function(element) {
if($('#method option:selected').text() == "Cash Deposit"){
return 20;
} else if($('#method option:selected').text() == "Wire Transfer"){
return 200;
}
else{
return 20;
}
}
}
,
max: maximumDA,
unique: true
},
HTML 的HTML
<select class="required" id="method" name="method" required>
<option value="deposit">Cash Deposit</option>
<option value="wire">Wire Transfer</option>
<option value="cbymail">Cash by Mail</option>
</select>
I'm trying to make the minimum dollar amount change when a different option is selected. 当尝试选择其他选项时,我正在尝试更改最低金额。 The depends does not work as is.
依赖项无法按原样工作。 I have a feeling I will have to create my own rule to get this to work, I am just curious to know if their is a way for depends to function correctly like this.
我有一种感觉,我必须创建自己的规则才能使它起作用,我只是好奇地知道它们是否是这样才能正常运行的方法。
EDIT: 编辑:
My Solution: 我的解决方案:
In order for the min amount to be dynamically changed, I don't think you can use a depends function to change a value of the rule, only the on off. 为了使最小金额能够动态更改,我认为您不能使用Depends函数来更改规则的值,而只能使用on off。
I simply removed and added the rules to make it work as I needed. 我只是删除并添加了规则以使其按需工作。
$('#method').change(function(){
if($('#method option:selected').text() == "Cash Deposit"){
$('#dollars').rules("remove","min");
$('#dollars').rules("add",{min:20});
}
else if($('#method option:selected').text() == "Wire Transfer"){
$('#dollars').rules("remove","min");
$('#dollars').rules("add",{min:200});
}
else{
$('#dollars').rules("remove","min");
$('#dollars').rules("add",{min:20});
}
});
first you should do when (events)change will happen in select tag , return that value.then only function will work. 首先,您应该在select标签中发生(事件)更改时执行该操作,返回该值,然后只有该函数才能工作。
if function inside of dollars.min it code does't know when it will be call. 如果在dollars.min内的函数,则代码不知道何时调用。
var minAmount;
$('#method').change(function(){
if($('#method option:selected').text() == "Cash Deposit") { minAmount=20; }
else if($('#method option:selected').text() == "Wire Transfer") { minAmount=200; }
else { minAmount=20; }
alert(minAmount);
});
now assign
minAmount value to dollars :{ min : minAmount }
现在
assign
minAmount值assign
给dollars :{ min : minAmount }
dollars: {
required: true,
number: true,
min:{ depends: minAmount },
max: maximumDA,
unique: true
},
This worked for me: 这为我工作:
function returnValue() {
var theValue;
if ($('#method option:selected').val() == "deposit") {
theValue = 20;
} else if ($('#method option:selected').val() == "wire") {
theValue = 200;
} else {
theValue = 20;
}
var variable = {
dollars: {
required: true,
number: true,
min: {
depends: theValue
},
max: maximumDA,
unique: true
}
};
return variable;
}
$('#method').change(function () {
returnValue();
});
returnValue();
Note that I made some changes to your dollars
property for testing purposes. 请注意,出于测试目的,我对您的
dollars
属性进行了一些更改。
ALTERNATIVE: 替代:
You can use: 您可以使用:
function returnValue() {
if ($('#method option:selected').val() == "deposit") {
return 20;
} else if ($('#method option:selected').val() == "wire") {
return 200;
} else {
return 20;
}
}
... and then call the function via: ...然后通过以下方式调用该函数:
min: {
depends: returnValue()
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.