简体   繁体   English

复选框onclick取消选中其他复选框

[英]Checkbox onclick uncheck other checkboxes

I have 6 checkboxes, one for each business day and one that says 'all'. 我有6个复选框,每个工作日一个,一个说“全部”。

What i want to be able to do is uncheck all the other boxes if someone clicks the 'all' checkbox if that makes sense. 我希望能够做的是取消选中所有其他框,如果有人点击“全部”复选框,如果这是有道理的。

For example, if someone has clicked monday and wednesday ... then they come in and click the 'all' checkbox, then the monday and wednesday checkbox should uncheck. 例如,如果有人点击星期一和星期三...然后他们进来并单击“全部”复选框,则应取消选中星期一和星期三复选框。

Cheers, 干杯,

This is not you want, but seems to be more sensible. 这不是你想要的,但似乎更明智。

HTML HTML

<input type="checkbox" id="chkAll" />
<br />
<input type="checkbox" id="chkMonday" class="child" />
<input type="checkbox" id="chkTuesday" class="child" />
<input type="checkbox" id="chkWednesday" class="child" />
<input type="checkbox" id="chkThursday" class="child" />
<input type="checkbox" id="chkFriday" class="child" />
<input type="checkbox" id="chkSaturday" class="child" />
<input type="checkbox" id="chkSunday" class="child" />

jQUery jQuery的

$(function(){
    $("#chkAll").change(function(){
        if (this.checked) {
            $("input:checkbox.child").attr("checked", "checked");
        }
        else {
            $("input:checkbox.child").removeAttr("checked");
        }
    });
});

See a working demo 查看工作演示

See an updated version which handles change in the child checkboxes also. 请参阅处理子项复选框的更新版本

jquery and some code like this should do it. jquery和这样的代码应该这样做。

$('#all-id').change(function(){
   if($('#all-id').is(':checked')){
     if($('#monday-id').is(':checked')){
        $('#monday-id').attr('checked', false);
     } else {
       $('#monday-id').attr('checked', true);
     }
     // etc
   }
});

After you might want to put all the ids in an array and setup a loop or play with the structure of your document to be able to easily loop on all of those checkboxes 您可能希望将所有ID放入数组并设置循环或播放文档结构后,可以轻松循环所有这些复选框

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM