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

前端 未结 29 2535
悲哀的现实
悲哀的现实 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:54

    You can use this simple code

    $('.checkall').click(function(){
        var checked = $(this).prop('checked');
        $('.checkme').prop('checked', checked);
    });
    
    0 讨论(0)
  • 2020-11-22 11:55
    <script language="JavaScript">
    function toggle(source) {
      checkboxes = document.getElementsByName('foo');
      for(var checkbox in checkboxes)
        checkbox.checked = source.checked;
    }
    </script>
    
    <input type="checkbox" onClick="toggle(this)" /> 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/>
    

    UPDATE:

    The for each...in construct doesn't seem to work, at least in this case, in Safari 5 or Chrome 5. This code should work in all browsers:

    function toggle(source) {
      checkboxes = document.getElementsByName('foo');
      for(var i=0, n=checkboxes.length;i<n;i++) {
        checkboxes[i].checked = source.checked;
      }
    }
    
    0 讨论(0)
  • 2020-11-22 11:56

    Using jQuery:

    // Listen for click on toggle checkbox
    $('#select-all').click(function(event) {   
        if(this.checked) {
            // Iterate each checkbox
            $(':checkbox').each(function() {
                this.checked = true;                        
            });
        } else {
            $(':checkbox').each(function() {
                this.checked = false;                       
            });
        }
    });
    

    HTML:

    <input type="checkbox" name="checkbox-1" id="checkbox-1" />
    <input type="checkbox" name="checkbox-2" id="checkbox-2" />
    <input type="checkbox" name="checkbox-3" id="checkbox-3" />
    
    <!-- select all boxes -->
    <input type="checkbox" name="select-all" id="select-all" />
    
    0 讨论(0)
  • 2020-11-22 11:57

    Using jQuery and knockout:

    With this binding main checkbox stays in sync with underliying checkboxes, it will be unchecked unless all checkboxes checked.

    ko.bindingHandlers.allChecked = {
      init: function (element, valueAccessor) {
        var selector = valueAccessor();
    
        function getChecked () {
          element.checked = $(selector).toArray().every(function (checkbox) {
            return checkbox.checked;
          });
        }
    
        function setChecked (value) {
          $(selector).toArray().forEach(function (checkbox) {
            if (checkbox.checked !== value) {
              checkbox.click();
            }
          });
        }
    
        ko.utils.registerEventHandler(element, 'click', function (event) {
          setChecked(event.target.checked);
        });
    
        $(window.document).on('change', selector, getChecked);
    
        ko.utils.domNodeDisposal.addDisposeCallback(element, () => {
          $(window.document).off('change', selector, getChecked);
        });
    
        getChecked();
      }
    };
    

    in html:

    <input id="check-all-values" type="checkbox" data-bind="allChecked: '.checkValue'"/>
    <input id="check-1" type="checkbox" class="checkValue"/>
    <input id="check-2" type="checkbox" class="checkValue"/>
    
    0 讨论(0)
  • 2020-11-22 11:59

    that should do the job done:

        $(':checkbox').each(function() {
            this.checked = true;                        
        });
    
    0 讨论(0)
提交回复
热议问题