使用jQuery选中所有复选框

Dar*_*yev 69 checkbox jquery css-selectors

我需要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>
Run Code Online (Sandbox Code Playgroud)

除了#select_all用户点击它之外,如何获取所有复选框?
任何帮助将不胜感激.

Tat*_*nen 106

一个更完整的示例应该适用于您的情况:

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

#select_all单击复选框时,将选中复选框的状态,并将当前表单中的所有复选框设置为相同的状态.

请注意,您不需要#select_all从选择中排除复选框,因为它将与所有其他复选框具有相同的状态.如果由于某种原因确实需要排除#select_all,可以使用:

var checkboxes = $(this).closest('form').find(':checkbox').not($(this));
Run Code Online (Sandbox Code Playgroud)

  • 对我来说,这只是第一次工作(即,检查所有,取消选中所有).在第二个'全部检查'中,没有选中其他复选框.我使用`checkboxes.prop('checked',true);`和`checkboxes.prop('checked',false);`来自[this answer](http://stackoverflow.com/a/15266603/1438809 ).哎呀,刚才意识到这在页面下方的答案中有所提及...... (14认同)
  • [jQuery文档](https://api.jquery.com/checkbox-selector/)声明`$("[type = checkbox]")`对于现代浏览器来说更快(比'$(':checkbox')更快`). (3认同)

小智 49

简单干净

$('#select_all').click(function() {
    var c = this.checked;
    $(':checkbox').prop('checked',c);
});
Run Code Online (Sandbox Code Playgroud)

  • 这也可以这样做......`$(':checkbox').prop('checked',this.checked);` (4认同)

ade*_*min 29

由于attr()方法,最终答案在Jquery 1.9+中不起作用.使用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);
        }
    });
});
Run Code Online (Sandbox Code Playgroud)


rah*_*hul 17

$("form input[type='checkbox']").attr( "checked" , true );
Run Code Online (Sandbox Code Playgroud)

或者你可以使用

:复选框选择器

$("form input:checkbox").attr( "checked" , true );
Run Code Online (Sandbox Code Playgroud)

我重写了你的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>
Run Code Online (Sandbox Code Playgroud)