问题描述
||
我正在将jQuery Validate用于表单。我需要添加一些功能,因此,如果从选择/下拉菜单中选择了特定选项,则需要一个附加字段。
这是他们在jQuery网站上提供的示例,如果在复选框(http://docs.jquery.com/Plugins/Validation/validate#options)中选中了一个选项,则需要certrain字段:
$(\".selector\").validate({
rules: {
contact: {
required: true,email: {
depends: function(element) {
return $(\"#contactform_email:checked\")
}
}
}
}
})
如果选择了特定选项,我将在其中显示其他字段的地方工作。一旦选择菜单失去焦点,控制台中将出现以下错误:
未捕获的TypeError:无法调用方法
未定义的\'call \'
我在JSfiddle上发布了一个示例。如果有人可以看看,我将不胜感激。我需要尽快开始工作!
http://jsfiddle.net/2EfPZ/
解决方法
修正了小提琴(并简化了一点):
http://jsfiddle.net/2EfPZ/21/
由于以下方面的出色表现:
ignore: \':hidden\'
,发生错误是因为您需要将depends
函数与特定的验证规则相关联:
$form.validate({
rules: {
favoriteSport: {
required: true
},position: {
required: {
depends: function(element) {
return $(\'#favoriteSport\').val() === \'1\';
}
}
},player: {
required: {
depends: function(element) {
return $(\"#favoriteSport\").val() === \'1\';
}
}
}
},messages: {
favoriteSport: \"You must select a favorite sport\",position: \"You must select a position because you selected football\",player: \"You must select a player because you selected football\"
}
});
工作示例:http://jsfiddle.net/2EfPZ/20/