jQuery Validator取决于方法未定义 [英] jQuery Validator depends method undefined
问题描述
Firebug向我显示以下内容:
Firebug is showing me the following:
从以下验证器初始化开始:
From the followin Validator initialization:
$("#surveyForm").validate({
errorPlacement: function(error, element) {
if ( element.is(":radio") ) {
error.appendTo ( '#' + element.attr('name') + '_multiError' );
} else if ( element.is(":checkbox") ) {
error.appendTo ( '#' + element.attr('name') + '_multiError' );
} else {
error.appendTo( element.parent() );
}
},
rules: {
ans_23: {
depends: function(element) {
return $("#ans_22:checked")
}
}
},
debug: true
});
该规则基于规则此处下的第二个示例.
The rule is based on the second example under rules here.
所引用的HTML看起来像这样
The HTML being referenced looks like this
<td class='two_columns'>
<label>
<input type='radio' name='rad_22' id='ans_22' class='required' value='Yes' /> Other
</label>
<input type='text' name='ans_23' id='ans_23' value='' class='' />
</td>
任何人都知道为什么depends方法将是未定义的吗?
Anyone know why the depends method would be undefined?
脚注:我还尝试使用规则添加方法(参见下文)进行此操作.当调用validate()并且ans_23没有获得"required"类时,该表单已经过验证,没有任何错误.
Footnote: I also tried doing this using the rule add method (see below). That form validated and threw no errors when validate() was called and ans_23 did not gain "required" class....
$("#surveyForm").rules("add", {
ans_23: {
required: "#ans_22:checked"
}
});
推荐答案
我看到您的评论回复将我的评论转换为答案...所以去了:
I saw a comment reply from you to convert my comment to an answer... so here goes:
$("#surveyForm").validate({
errorPlacement: function(error, element) {
if ( element.is(":radio") ) {
error.appendTo ( '#' + element.attr('name') + '_multiError' );
} else if ( element.is(":checkbox") ) {
error.appendTo ( '#' + element.attr('name') + '_multiError' );
} else {
error.appendTo( element.parent() );
}
},
rules: {
ans_23: {
required: {
depends: function(element) {
return $("#ans_22:checked");
}
}
}
},
debug: true
});
这篇关于jQuery Validator取决于方法未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!