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

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

    I'm not sure anyone hasn't answered in this way (using jQuery):

      $( '#container .toggle-button' ).click( function () {
        $( '#container input[type="checkbox"]' ).prop('checked', this.checked)
      })
    

    It's clean, has no loops or if/else clauses and works as a charm.

    0 讨论(0)
  • 2020-11-22 11:34
    <html>
    
    <head>
    <script type="text/javascript">
    
        function do_this(){
    
            var checkboxes = document.getElementsByName('approve[]');
            var button = document.getElementById('toggle');
    
            if(button.value == 'select'){
                for (var i in checkboxes){
                    checkboxes[i].checked = 'FALSE';
                }
                button.value = 'deselect'
            }else{
                for (var i in checkboxes){
                    checkboxes[i].checked = '';
                }
                button.value = 'select';
            }
        }
    </script>
    </head>
    
    <body>
    <input type="checkbox" name="approve[]" value="1" />
    <input type="checkbox" name="approve[]" value="2" />
    <input type="checkbox" name="approve[]" value="3" />
    
    <input type="button" id="toggle" value="select" onClick="do_this()" />
    </body>
    
    </html>
    
    0 讨论(0)
  • 2020-11-22 11:34

    JavaScript is your best bet. The link below gives an example using buttons to de/select all. You could try to adapt it to use a check box, just use you 'select all' check box' onClick attribute.

    Javascript Function to Check or Uncheck all Checkboxes

    This page has a simpler example

    http://www.htmlcodetutorial.com/forms/_INPUT_onClick.html

    0 讨论(0)
  • 2020-11-22 11:35
    $(document).ready(function() {
                    $(document).on(' change', 'input[name="check_all"]', function() {
                        $('.cb').prop("checked", this.checked);
                    });
                });
    
    0 讨论(0)
  • 2020-11-22 11:35

    html

    <input class='all' type='checkbox'> All
    <input class='item' type='checkbox' value='1'> 1
    <input class='item' type='checkbox' value='2'> 2
    <input class='item' type='checkbox' value='3'> 3
    

    javascript

    $(':checkbox.all').change(function(){
      $(':checkbox.item').prop('checked', this.checked);
    });
    
    0 讨论(0)
  • 2020-11-22 11:36

    This is what this will do, for instance if you have 5 checkboxes, and you click check all,it check all, now if you uncheck all the checkbox probably by clicking each 5 checkboxs, by the time you uncheck the last checkbox, the select all checkbox also gets unchecked

    $("#select-all").change(function(){
       $(".allcheckbox").prop("checked", $(this).prop("checked"))
      })
      $(".allcheckbox").change(function(){
          if($(this).prop("checked") == false){
              $("#select-all").prop("checked", false)
          }
          if($(".allcheckbox:checked").length == $(".allcheckbox").length){
              $("#select-all").prop("checked", true)
          }
      })
    
    0 讨论(0)
提交回复
热议问题