简体   繁体   English

jQuery从全选中排除复选框

[英]Jquery Exclude a checkbox from select all

My HTML code to select all checkboxes 我的HTML代码以选中所有复选框

Select All <input type="checkbox"  name='select_all' id='select_all'  value='1'/>

My Javascript code to select all checkboxes 我的Javascript代码以选中所有复选框

<script type="text/javascript">
    $('#select_all').change(function() {
    var checkboxes = $(this).closest('form').find(':checkbox');
    if($(this).is(':checked')) 
    {
       checkboxes.prop('checked', true);
                } else {
        checkboxes.prop('checked', false);
        }
    });
    </script>

The code works great and selects all the checkboxes, I would like to exclude the following checkbox from the selection (select all) criteria, is it possible to exclude the following? 该代码很好用,并选中了所有复选框,我想从选择(全选)条件中排除以下复选框,是否可以排除以下内容?

 <input type="checkbox"  name='sendtoparent' id='sendtoparent'  value='1'/>

尝试使用not方法:

$(this).closest('form').find(':checkbox').not('#sendtoparent');

Firstly you can use :not or not() to exclude the element by its id attribute. 首先,您可以使用:notnot()通过其id属性排除该元素。 From there you can simplify the logic by just setting the checked property of those checkboxes to match that of the #select_all element. 在这里,您只需设置这些复选框的checked属性以匹配#select_all元素的属性即可简化逻辑。 Try this: 尝试这个:

$('#select_all').change(function() {
  var $checkboxes = $(this).closest('form').find(':checkbox').not('#sendtoparent');
  $checkboxes.prop('checked', this.checked);
});

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

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