【发布时间】:2011-11-02 16:47:23
【问题描述】:
使用这里找到的 jQuery 插件:
http://docs.jquery.com/Plugins/Validation
我有一个表单,即选择某些复选框时,向用户显示其他字段。我想在他们点击复选框时将这些字段设为必填,如果取消选中则不需要。
有没有办法动态设置元素的要求?
【问题讨论】:
标签: jquery validation
使用这里找到的 jQuery 插件:
http://docs.jquery.com/Plugins/Validation
我有一个表单,即选择某些复选框时,向用户显示其他字段。我想在他们点击复选框时将这些字段设为必填,如果取消选中则不需要。
有没有办法动态设置元素的要求?
【问题讨论】:
标签: jquery validation
我认为最简单的方法是在点击时使用 jquery 将“必需”类附加到这些元素。
【讨论】:
只是为了扩展 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 类是不言自明的 :)
【讨论】: