jquery checkbox select all and deselect can only be executed once, and it will fail the second time [best solution]

[Phenomena]: jquery checkbox can only be executed once, and it will fail the second time

 

$("#checkbox").on('click', function (e) {  
    $("input[name='checkbox']").each(function(){
       if($(this).attr("checked")){
          $(this).removeAttr("checked");
       }else{
          $(this).attr("checked","true");
       }
    });
});  

 

【solution】:

 

 

$("#checkbox").on('click', function (e) {  
        var isChecked = $(this).prop("checked");  
        $("input[name='checkbox']").prop("checked", isChecked);  
});  

 

 

Donor sharer

          I didn't like programming before, but now I am an IT fan obsessed with programming. I would like to share some things I have sorted out and optimized here, hoping to help IT fans, with joy and sweat, and at the same time I also hope that everyone can support it. Of course, if you have money to support a money field (support Alipay and WeChat donations, join the it data center buckle group), but have no money to support a personal field, with your support, we will be more motivated and do better, thank you Ladies and gentlemen.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326665188&siteId=291194637