Jquery “select all” checkbox

后端 未结 11 613
执念已碎
执念已碎 2020-12-06 05:45

I am trying to select all checkboxes on a page once the \'select all\' checkbox is clicked and i am using jquery for this as you can see at the below link:

http://js

相关标签:
11条回答
  • 2020-12-06 06:22

    Use this...

    $('#selectall').on('click', function() {
        $('.selectedId').attr('checked', $(this).is(":checked"));
    });
    

    An see this DEMO

    0 讨论(0)
  • 2020-12-06 06:25

    $(function(){

    // add multiple select / deselect functionality
    $("#selectall").click(function () {
          $('.case').attr('checked', this.checked);
    });
    
    // if all checkbox are selected, check the selectall checkbox
    // and viceversa
    $(".case").click(function(){
    
        if($(".case").length == $(".case:checked").length) {
            $("#selectall").attr("checked", "checked");
        } else {
            $("#selectall").removeAttr("checked");
        }
    
    });
    

    });

    0 讨论(0)
  • 2020-12-06 06:28

    Why don't you do it this way? It is more clear and readable

    $('#selectall').click(function () {
        $('.selectedId').prop('checked', this.checked);
    });
    
    $('.selectedId').change(function () {
        var check = ($('.selectedId').filter(":checked").length == $('.selectedId').length);
        $('#selectall').prop("checked", check);
    });
    

    DEMO

    0 讨论(0)
  • 2020-12-06 06:29

    More specific solution:

    $(document).ready(function(){
      $('#checkAll').on('click', function ( e ) {
        $('input[name="chck_box[]"]').prop('checked', $(this).is(':checked'));
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <table>
      <thead>
        <th><input type="checkbox" id="checkAll"></th>
        <th>Name</th>
        <th>Last</th>
      </thead>
      <tbody>
        <tr>
          <td><input type="checkbox" name="chck_box[]" value="1"></td>
          <td>Joe</td>
          <td>Doe</td>
        </tr>
        <tr>
          <td><input type="checkbox" name="chck_box[]" value="2"></td>
          <td>John</td>
          <td>Smith</td>
        </tr>
        <tr>
          <td><input type="checkbox" name="chck_box[]" value="3"></td>
          <td>Bill</td>
          <td>White</td>
        </tr>
      </tbody>
    </table>

    0 讨论(0)
  • 2020-12-06 06:30

    A small enhancement to @letiagoalves solution, if your checkboxes have specific processing associated with a click event. Not asked for by the OP, but something I've had to do in the past. Basically, instead of directly setting the checked property of each dependent checkbox, it compares the checked state of each dependent checkbox to the selectall checkbox, and triggers a click event on those that are in the opposite state.

    $('#selectall').click(function () {
        var parentState = $(this).prop('checked');
        $('.selectedId').each(function() {
            if ($(this).prop('checked') !== parentState) {
                $(this).trigger("click");
            }
    });
    });
    

    DEMO: https://jsfiddle.net/jajntuqb/2/

    0 讨论(0)
提交回复
热议问题