第二个方法中的 $(this).val() 返回与第一个方法中相同的值。我希望通过 secondGroup 类获得字段的第一个值。我做错了什么?
$(document).ready(function(){
jQuery.validator.addMethod("method1", function(value, element, options) {
.....some code here....
var elems = $(element).parents('form').find(options[0]);
jQuery.each(elems, function(){
thisVal = $(this).val();
});
.....some code here......
}, jQuery.format("some message."));
jQuery.validator.addMethod("method2", function(value, element, options) {
.....some code here....
var elems = $(element).parents('form').find(options[0]);
jQuery.each(elems, function(){
thisVal = $(this).val();
});
.....some code here......
}, jQuery.format("some message."));
$("#formName").validate({
rules: {
firstMethod1:{
method1: ['.firstGroup']
},
secondMethod1:{
method1: ['.firstGroup']
},
thirdMethod1:{
method1: ['.firstGroup']
},
firstMethod2:{
method2: ['.secondGroup']
},
secondMethod2:{
method2: ['.secondGroup']
},
thirdMethod2:{
method2: ['.secondGroup']
}
}
});
});
最佳答案
您正在使用 jQuery.each()
而不是 .each()
。
使用:
elems.each(function(){
thisVal = $(this).val();
});
关于javascript - 为什么 $(this).val() 在第二种方法中给我第一种方法的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6334166/