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

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

    Here is a backbone.js implementation:

    events: {
        "click #toggleChecked" : "toggleChecked"
    },
    toggleChecked: function(event) {
    
        var checkboxes = document.getElementsByName('options');
        for(var i=0; i<checkboxes.length; i++) {
            checkboxes[i].checked = event.currentTarget.checked;
        }
    
    },
    
    0 讨论(0)
  • 2020-11-22 11:50

    Simple and to the POINT:

    jQuery - Upon Clicking a button or div or a label element. Check off all checkboxes on the page. Keep in mind you'll have to adjust :checkbox to make it more specific.

    jQuery("#My-Button").click(function() {
    
      jQuery(':checkbox').each(function() {
        if(this.checked == true) {
          this.checked = false;                        
        } else {
          this.checked = true;                        
        }      
      });
    
    });
    
    0 讨论(0)
  • 2020-11-22 11:51

    Below methods are very Easy to understand and you can implement existing forms in minutes

    With Jquery,

    $(document).ready(function() {
      $('#check-all').click(function(){
        $("input:checkbox").attr('checked', true);
      });
      $('#uncheck-all').click(function(){
        $("input:checkbox").attr('checked', false);
      });
    });
    
    

    in HTML form put below buttons

    <a id="check-all" href="javascript:void(0);">check all</a>
    <a id="uncheck-all" href="javascript:void(0);">uncheck all</a> 
    

    With just using javascript,

    <script type="text/javascript">
    function checkAll(formname, checktoggle)
    {
      var checkboxes = new Array(); 
      checkboxes = document[formname].getElementsByTagName('input');
    
      for (var i=0; i<checkboxes.length; i++)  {
        if (checkboxes[i].type == 'checkbox')   {
          checkboxes[i].checked = checktoggle;
        }
      }
    }
    </script>
    

    in HTML form put below buttons

    <button onclick="javascript:checkAll('form3', true);" href="javascript:void();">check all</button>
    
    <button onclick="javascript:checkAll('form3', false);" href="javascript:void();">uncheck all</button>
    
    
    0 讨论(0)
  • 2020-11-22 11:52

    1: Add the onchange event Handler

    <th><INPUT type="checkbox" onchange="checkAll(this)" name="chk[]" /> </th>
    

    2: Modify the code to handle checked/unchecked

     function checkAll(ele) {
         var checkboxes = document.getElementsByTagName('input');
         if (ele.checked) {
             for (var i = 0; i < checkboxes.length; i++) {
                 if (checkboxes[i].type == 'checkbox') {
                     checkboxes[i].checked = true;
                 }
             }
         } else {
             for (var i = 0; i < checkboxes.length; i++) {
                 console.log(i)
                 if (checkboxes[i].type == 'checkbox') {
                     checkboxes[i].checked = false;
                 }
             }
         }
     }
    
    0 讨论(0)
  • 2020-11-22 11:52

    to make it in short-hand version by using jQuery

    The select all checkbox

    <input type="checkbox" id="chkSelectAll">
    

    The children checkbox

    <input type="checkbox" class="chkDel">
    <input type="checkbox" class="chkDel">
    <input type="checkbox" class="chkDel">
    

    jQuery

    $("#chkSelectAll").on('click', function(){
         this.checked ? $(".chkDel").prop("checked",true) : $(".chkDel").prop("checked",false);  
    })
    
    0 讨论(0)
  • 2020-11-22 11:54

    Try this simple JQuery:

    $('#select-all').click(function(event) {
      if (this.checked) {
        $(':checkbox').prop('checked', true);
      } else {
        $(':checkbox').prop('checked', false);
      }
    });
    
    0 讨论(0)
提交回复
热议问题