简体   繁体   English

如果选中另一个复选框,如何取消选中一组复选框

[英]How to uncheck a group of checkboxes when another checkbox is checked

I've created a search function on a website I'm developing but I'm stuck on this part. 我在我正在开发的网站上创建了一个搜索功能,但我仍然坚持这一部分。 The search function has a list of categories which the user can check or uncheck in order to filter through the items. 搜索功能有一个类别列表,用户可以检查或取消选中这些类别以过滤项目。 The options are almost identical to the way Coursera has implemented their search function. 这些选项几乎与Coursera实现其搜索功能的方式相同。 I would like to have all the other checkboxes unchecked when the All Categories option is checked and have the All Categories checkbox unchecked when anything else is unchecked. 我希望在选中“ 所有类别”选项时取消选中所有其他复选框,并在取消选中其他任何内容时取消选中“ 所有类别”复选框。 That sounds a bit confusing but it's exactly what that website has implemented. 这听起来有点令人困惑,但它正是该网站实施的内容。

Here's an example of the code: 这是代码的示例:

<div class="filter-grade">
    <label class="checkbox">
        <input type="checkbox" id="grade-all" name="grade[]" value="0" checked><b>All Grades</b>
    </label>
    <label class="checkbox">
        <input type="checkbox" id="grade-9" name="grade[]" value="9">Grade 9
    </label>
    <label class="checkbox">
        <input type="checkbox" id="grade-10" name="grade[]" value="10">Grade 10
    </label>
    <label class="checkbox">
        <input type="checkbox" id="grade-11" name="grade[]" value="11">Grade 11
    </label>
    <label class="checkbox">
        <input type="checkbox" id="grade-12" name="grade[]" value="12">Grade 12
    </label>
</div>

Despite my comment, I built out a fiddle: http://jsfiddle.net/ctnCn/5/ 尽管我的评论,我还是建立了一个小提琴: http//jsfiddle.net/ctnCn/5/

$('.group input').on('change', function() {
    var $inputs = $(this).siblings('input').add(this),
        $checked = $inputs.filter(':checked'),
        $all = $inputs.eq(0),
        index = $inputs.index(this);

    (index === 0 ? $checked : $all).removeAttr('checked');

    if(index === 0 || $checked.length === 0) {
        $all.attr('checked', 'checked');
    }
});

Update to be project specific: http://jsfiddle.net/ctnCn/6/ 更新为特定项目: http//jsfiddle.net/ctnCn/6/

$('.filter-grade input').on('change', function() {
    var $inputs = $(this).closest('div').find('input'),

Though I would like to point out that this is not the standard usage of the label element. 虽然我想指出这不是标签元素的标准用法。 I would suggest changing your markup to have labels and inputs be siblings (and use the "id" and "for" attributes to link them). 我建议更改你的标记,使标签和输入成为兄弟姐妹(并使用“id”和“for”属性来链接它们)。 Take a look at this reference: http://www.w3schools.com/tags/tag_label.asp My previous fiddle also provides an example of the standard markup convention. 看看这个参考: http//www.w3schools.com/tags/tag_label.asp我以前的小提琴也提供了标准标记约定的一个例子。

This code also doing the trick .Get in the right path from the answer of ArraryKnigh. 这段代码也在做伎俩。从ArraryKnigh的答案中获取正确的路径。 You can also check the fiddle : 你也可以检查小提琴

$('.group :checkbox').filter("[class!='group-all']").on('change', function() {
     var parentDiv=$(this).parent();
    $('.group-all',parentDiv ).attr('checked',false);
 });

$('.group :checkbox').filter("[class='group-all']").on('change', function() {
    var parentDiv=$(this).parent();
    $('input',parentDiv ).attr('checked',false);
    $(this).attr('checked',true);
 });

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

相关问题 当组中的一个复选框被选中时,禁用并取消选中所有其他复选框 - When one checkbox is checked in a group, disable & uncheck all other checkboxes 如何选中另一个复选框时取消选中复选框? - How to uncheck a checkbox when another one is checked? 如何在选中单选按钮时取消选中复选框,以及在使用Javascript选中复选框时如何取消选中单选按钮 - How to uncheck checkbox when radio button is checked and uncheck radio button when checkboxes are checked using Javascript 复选框:取消选中时选中另一个 - Checkbox: uncheck when on another is checked 选中另一个复选框后,取消选中该复选框 - Uncheck a checkbox when another is checked 如果选中的复选框超过3个,如何取消选中该复选框 - How to uncheck a checkbox if there are checked more than 3 checkboxes 如果勾选了一个复选框,如何取消选中所有复选框? - How to uncheck all checkboxes, if one checkbox checked? 当另一个复选框被选中时取消选中一个复选框 - Uncheck a checkbox when another checkbox is checked 当“ Any”复选框被选中无法使用时,取消选中组中的所有复选框 - Uncheck all checkboxes in a group when “Any” checkbox is checked used to work not working anymore 选中另一个复选框后如何取消选中默认复选框-Angular - How To Uncheck Default Checkbox When Another Is Checked - Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM