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

前端 未结 29 3033
悲哀的现实
悲哀的现实 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: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)
          }
      })
    

提交回复
热议问题