使用jQuery插件发现这里:jquery验证,使字段有条件地需要?
http://docs.jquery.com/Plugins/Validation
我有一个形式,即当选择一定的选择框,附加字段被示出给用户。我希望在点击复选框时使这些字段成为必需字段,如果不选中它们,则不需要。
有没有办法为元素动态设置需求?
使用jQuery插件发现这里:jquery验证,使字段有条件地需要?
http://docs.jquery.com/Plugins/Validation
我有一个形式,即当选择一定的选择框,附加字段被示出给用户。我希望在点击复选框时使这些字段成为必需字段,如果不选中它们,则不需要。
有没有办法为元素动态设置需求?
我认为最简单的方法是在点击时将使用jquery的“required”类附加到这些元素上。
只是为了通过K2so对答案拓展上面:
如果你把这样的文件准备:
$("#reasonCode").change(reasonCodeChange);
其中id为reasonCode元素相当于你的复选框。在更改时,它会调用函数reasonCodeChange ...
function reasonCodeChange(){
var strValue = $("#reasonCode option:selected").text()
strValue = strValue.substring(0,2);
if(strValue== "7." || strValue== "9." || strValue== "10" ||strValue== "11" || strValue== "12"){
$('#reasonCodeExpRow').addClass('showMe');
$('#reasonCodeExp').addClass('required');
$('#reasonCodeExpRow').removeClass('hideMe');
}else{
$('#reasonCodeExpRow').addClass('hideMe');
$('#reasonCodeExp').removeClass('required');
$('#reasonCodeExpRow').removeClass('showMe');
};
}
而这应该足以让你去。 hideMe和showMe类是不言自明的:)