Check/Uncheck all the checkboxes in a table

后端 未结 7 2139
被撕碎了的回忆
被撕碎了的回忆 2020-12-07 23:28

I have a table with information. The first column of the table have checkboxes. I can add/delete rows with a button by checking the checkboxes. The problem I have now is how

7条回答
  •  清歌不尽
    2020-12-07 23:40

    Actually your checkAll(..) is hanging without any attachment.

    1) Add onchange event handler

     
    

    2) Modified the code to handle check/uncheck

     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;
                 }
             }
         }
     }
    

    Updated Fiddle

提交回复
热议问题