我有很多字段就是这样创建的
<input type="text" name="coeff_a">
<input type="text" name="coeff_b">
<input type="text" name="coeff_c"> .. and so on ..
我想验证上述字段的输入,并使用jQuery validate插件。我已经设置了这样的规则
jQuery.validator.addMethod(
"perc",
function(value, element) {
// all the fields that start with 'coeff'
var coeff = $("input[name^='coeff']");
var total;
for (var i = 0; i < coeff.length; i++) {
total += coeff[i].value;
}
if (total <= 100) {
return true;
}
return false;
},
jQuery.format("Please ensure percentages don't add up to more than 100")
);
毋庸置疑,上述情况并不奏效。我有什么想法吗?
答案 0 :(得分:1)
您的功能未返回正确的值。
function cals(value, element) {
// all the fields that start with 'coeff'
var coeff = $("input[name^='coeff']");
var total = 0;
for (var i = 0; i < coeff.length; i++) {
total += Number(coeff[i].value);
}
if (total <= 100) {
return true;
}
return false;
}
编辑了一些东西。看看吧。