【发布时间】:2012-06-16 19:33:41
【问题描述】:
在 StackOverflow 上尝试了多个示例来解决我的问题后,我仍然无法弄清楚我的问题。
我有 3 个复选框:
<input id="pets_dog" class="text" type="checkbox" class="pets" name="pets[]" value="pets_dog"> Dogs</input>
<input id="pets_cats" class="text" type="checkbox" class="pets" name="pets[]" value="pets_cats"> Cats</input>
<input id="pets_none" class="text" type="checkbox" class="pets" name="pets[]" value="pets_no"> None</input>
当“pets_none”被选中时,我需要取消选中其他 2 个复选框,反之亦然。这是我当前的 jQuery 代码:
$("#pets_none").change(function() {
if($("#pets_none").attr('checked')) {
$("#pets_cats").removeAttr('checked');
$("#pets_dogs").removeAttr('checked');
}
});
我一生都无法弄清楚为什么它不起作用,并感谢任何帮助。
【问题讨论】:
-
您不应分配 2 个类属性,因为这是无效的 Html。例如,拥有 2 个类属性还会阻止您按
pets类选择您的输入。即:$(.pets)将不起作用,因为.text优先。如果你想分配 2 个班级,请这样做class="text pets"
标签: javascript jquery html checkbox