How to implement “select all” check box in HTML?

前端 未结 29 2537
悲哀的现实
悲哀的现实 2020-11-22 11:09

I have an HTML page with multiple checkboxes.

I need one more checkbox by the name \"select all\". When I select this checkbox all checkboxes in the HTML page must b

相关标签:
29条回答
  • 2020-11-22 11:39

    Slightly changed version which checks and unchecks respectfully

    $('#select-all').click(function(event) {
            var $that = $(this);
            $(':checkbox').each(function() {
                this.checked = $that.is(':checked');
            });
        });
    
    0 讨论(0)
  • 2020-11-22 11:40

    You may have different sets of checkboxes on the same form. Here is a solution that selects/unselects checkboxes by class name, using vanilla javascript function document.getElementsByClassName

    The Select All button

    <input type='checkbox' id='select_all_invoices' onclick="selectAll()"> Select All
    

    Some of the checkboxes to select

    <input type='checkbox' class='check_invoice' id='check_123' name='check_123' value='321' />
    <input type='checkbox' class='check_invoice' id='check_456' name='check_456' value='852' />
    

    The javascript

        function selectAll() {
            var blnChecked = document.getElementById("select_all_invoices").checked;
            var check_invoices = document.getElementsByClassName("check_invoice");
            var intLength = check_invoices.length;
            for(var i = 0; i < intLength; i++) {
                var check_invoice = check_invoices[i];
                check_invoice.checked = blnChecked;
            }
        }
    
    0 讨论(0)
  • 2020-11-22 11:43

    To make it deselect:

    $('#select_all').click(function(event) {
      if(this.checked) {
          // Iterate each checkbox
          $(':checkbox').each(function() {
              this.checked = true;
          });
      }
      else {
        $(':checkbox').each(function() {
              this.checked = false;
          });
      }
    });
    
    0 讨论(0)
  • 2020-11-22 11:43

    I'm surprised no one mentioned document.querySelectorAll(). Pure JavaScript solution, works in IE9+.

    function toggle(source) {
        var checkboxes = document.querySelectorAll('input[type="checkbox"]');
        for (var i = 0; i < checkboxes.length; i++) {
            if (checkboxes[i] != source)
                checkboxes[i].checked = source.checked;
        }
    }
    <input type="checkbox" onclick="toggle(this);" />Check all?<br />
    
    <input type="checkbox" />Bar 1<br />
    <input type="checkbox" />Bar 2<br />
    <input type="checkbox" />Bar 3<br />
    <input type="checkbox" />Bar 4<br />

    0 讨论(0)
  • 2020-11-22 11:44

    When you call document.getElementsByName("name"), you will get a Object. Use .item(index) to traverse all items of a Object

    HTML:

    <input type="checkbox" onclick="for(c in document.getElementsByName('rfile')) document.getElementsByName('rfile').item(c).checked = this.checked">
    
    <input type=​"checkbox" name=​"rfile" value=​"/​cgi-bin/​">​
    <input type=​"checkbox" name=​"rfile" value=​"/​includes/​">​
    <input type=​"checkbox" name=​"rfile" value=​"/​misc/​">​
    <input type=​"checkbox" name=​"rfile" value=​"/​modules/​">​
    <input type=​"checkbox" name=​"rfile" value=​"/​profiles/​">​
    <input type=​"checkbox" name=​"rfile" value=​"/​scripts/​">​
    <input type=​"checkbox" name=​"rfile" value=​"/​sites/​">​
    <input type=​"checkbox" name=​"rfile" value=​"/​stats/​">​
    <input type=​"checkbox" name=​"rfile" value=​"/​themes/​">​
    
    0 讨论(0)
  • 2020-11-22 11:44

    As I cannot comment, here as answer: I would write Can Berk Güder's solution in a more general way, so you may reuse the function for other checkboxes

    <script language="JavaScript">
      function toggleCheckboxes(source, cbName) {
        checkboxes = document.getElementsByName(cbName);
        for (var i = 0, n = checkboxes.length; i < n; i++) {
          checkboxes[i].checked = source.checked;
        }
      }
    </script>
    
    <input type="checkbox" onClick="toggleCheckboxes(this,\'foo\')" /> Toggle All<br/>
    
    <input type="checkbox" name="foo" value="bar1"> Bar 1<br/>
    <input type="checkbox" name="foo" value="bar2"> Bar 2<br/>
    <input type="checkbox" name="foo" value="bar3"> Bar 3<br/>
    <input type="checkbox" name="foo" value="bar4"> Bar 4<br/>
    <input type="checkbox" name="foo" value="bar5"> Bar 5<br/>
    
    0 讨论(0)
提交回复
热议问题