代码之家  ›  专栏  ›  技术社区  ›  Darmen Amanbay Cherma Ramalho

使用jquery选择所有复选框

  •  64
  • Darmen Amanbay Cherma Ramalho  · 技术社区  · 15 年前

    我需要jquery选择器的帮助。假设我有如下所示的标记:

    <form>
        <table>
            <tr>
                <td><input type="checkbox" id="select_all"/></td>
            </tr>
            <tr>
                <td><input type="checkbox" name="select[]"/></td>
            </tr>
            <tr>
                <td><input type="checkbox" name="select[]"/></td>
            </tr>
            <tr>
                <td><input type="checkbox" name="select[]"/></td>
            </tr>
        </table>
    </form>
    

    如何获取除 #select_all 当用户点击它时?
    如有任何帮助,我们将不胜感激。

    15 回复  |  直到 6 年前
        1
  •  99
  •   Huw Jones    7 年前

    一个更完整的例子应该适用于您的案例:

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

    #select_all 复选框被单击,复选框的状态被选中,并且当前窗体中的所有复选框都被设置为相同的状态。

    请注意,您不需要排除 第二选择 选中的复选框将与所有其他复选框具有相同的状态。如果出于某种原因需要排除 #全选 ,您可以使用:

    var checkboxes = $(this).closest('form').find(':checkbox').not($(this));
    
        2
  •  46
  •   LOL    13 年前

    简单而干净

    $('#select_all').click(function() {
        var c = this.checked;
        $(':checkbox').prop('checked',c);
    });
    
        3
  •  29
  •   alexeydemin    11 年前

    因为attr()方法,所以在jquery 1.9+中,top-answer将不起作用。改用prop():

    $(function() {
        $('#select_all').change(function(){
            var checkboxes = $(this).closest('form').find(':checkbox');
            if($(this).prop('checked')) {
              checkboxes.prop('checked', true);
            } else {
              checkboxes.prop('checked', false);
            }
        });
    });
    
        4
  •  15
  •   Adeel Kirti Nariya    7 年前
    $("form input[type='checkbox']").attr( "checked" , true );
    

    或者你可以使用

    :checkbox Selector

    $("form input:checkbox").attr( "checked" , true );
    

    我已经重写了您的HTML并为主复选框提供了一个单击处理程序

    $(function(){
        $("#select_all").click( function() {
            $("#frm1 input[type='checkbox'].child").attr( "checked", $(this).attr("checked" ) );
        });
    });
    
    <form id="frm1">
        <table>
            <tr>
                <td>
                    <input type="checkbox" id="select_all" />
                </td>
            </tr>
            <tr>
                <td>
                    <input type="checkbox" name="select[]" class="child" />
                </td>
            </tr>
            <tr>
                <td>
                    <input type="checkbox" name="select[]" class="child" />
                </td>
            </tr>
            <tr>
                <td>
                    <input type="checkbox" name="select[]" class="child" />
                </td>
            </tr>
        </table>
    </form>
    
        5
  •  3
  •   Dragos Custura    12 年前
     $(function() {  
    $('#select_all').click(function() {
        var checkboxes = $(this).closest('form').find(':checkbox');
        if($(this).is(':checked')) {
            checkboxes.attr('checked', 'checked');
        } else {
            checkboxes.removeAttr('checked');
        }
    });
        });
    
        6
  •  3
  •   Beena Shetty    12 年前
     $(document).ready(function(){
        $("#select_all").click(function(){
          var checked_status = this.checked;
          $("input[name='select[]']").each(function(){
            this.checked = checked_status;
          });
        });
      });
    
        7
  •  3
  •   Abdo-Host    9 年前
    jQuery(document).ready(function () {
            jQuery('.select-all').on('change', function () {
                if (jQuery(this).is(':checked')) {
                    jQuery('input.class-name').each(function () {
                        this.checked = true;
                    });
                } else {
                    jQuery('input.class-name').each(function () {
                        this.checked = false;
                    });
                }
            });
        });
    
        8
  •  2
  •   Ahmed Bermawy    9 年前

    这个代码对我很有用

    <script type="text/javascript">
    $(document).ready(function(){ 
    $("#select_all").change(function(){
      $(".checkbox_class").prop("checked", $(this).prop("checked"));
      });
    });
    </script>
    

    你只需要添加类 检查框类 对所有复选框

    简单易行:d

        9
  •  2
  •   Community CDub    7 年前
    $("#select_all").change(function () {
    
        $('input[type="checkbox"]').prop("checked", $(this).prop("checked"));
    
    });  
    
        10
  •  1
  •   Odin Thunder Macci001    8 年前

    面对这个问题,上述答案都不起作用。原因是jquery统一插件(使用 theme metronic )我希望答案会有用:)

    一起工作 JQuery统一

    $('#select-all').change(function() {
        var $this = $(this);
        var $checkboxes = $this.closest('form')
            .find(':checkbox');
    
        $checkboxes.prop('checked', $this.is(':checked'))
            .not($this)
            .change();
    });
    
        11
  •  1
  •   nass    7 年前

    一个复选框来控制它们

    对于仍在通过一个轻量级的插件来寻找控制复选框的插件,该插件对uniformjs和icheck具有开箱即用的支持,并且在至少一个受控复选框未选中时未选中(当然,当所有受控复选框都选中时也会选中),我创建了一个 jQuery checkAll plugin .

    请随意查看以下示例 documentation page .


    对于这个问题示例,您需要做的就是:

    $( '#select_all' ).checkall({
        target: 'input[type="checkbox"][name="select"]'
    });
    

    这不是很清楚吗?

        12
  •  0
  •   antyrat Andy    13 年前
    $('.checkall').change(function() {
        var checkboxes = $(this).closest('table').find('td').find(':checkbox');
        if($(this).is(':checked')) {
            checkboxes.attr('checked', 'checked');
        } else {
            checkboxes.removeAttr('checked');
        }
    });
    
        13
  •  0
  •   user2821486    11 年前
      $("#select_all").live("click", function(){
                $("input").prop("checked", $(this).prop("checked"));
        }
      });
    
        14
  •  0
  •   FlipMcF    9 年前

    我现在偏爱这种风格。

    我已经为您的表单命名,并在“全选”框中添加了一个“onclick”。

    我还排除了jquery选择器中的“全选”复选框,以防止有人单击它时互联网爆炸。

     function toggleSelect(formname) {
       // select the form with the name 'formname', 
       // then all the checkboxes named 'select[]'
       // then 'click' them
       $('form[name='+formname+'] :checkbox[name="select[]"]').click()
     }
    

     <form name="myform">
       <tr>
            <td><input type="checkbox" id="select_all"    
                       onClick="toggleSelect('myform')" />
            </td>
        </tr>
        <tr>
            <td><input type="checkbox" name="select[]"/></td>
        </tr>
        <tr>
            <td><input type="checkbox" name="select[]"/></td>
        </tr>
        <tr>
            <td><input type="checkbox" name="select[]"/></td>
        </tr>
    </table>
    

        15
  •  0
  •   Gareth Daine    9 年前

    下面是我编写的一个基本jquery插件,它选择页面上的所有复选框,除了用作切换的复选框/元素:

    (function($) {
        // Checkbox toggle function for selecting all checkboxes on the page
        $.fn.toggleCheckboxes = function() {
            // Get all checkbox elements
            checkboxes = $(':checkbox').not(this);
    
            // Check if the checkboxes are checked/unchecked and if so uncheck/check them
            if(this.is(':checked')) {
                checkboxes.prop('checked', true);
            } else {
                checkboxes.prop('checked', false);
            }
        }
    }(jQuery));
    

    然后简单地调用复选框或按钮元素上的函数:

    // Check all checkboxes
    $('.check-all').change(function() {
        $(this).toggleCheckboxes();
    });