简体   繁体   中英

How to get a count of checkboxes checked on pagination Jquery dataTables

Below is my code, I want to get the count of checked checkboxes of all pages in my DataTable. Please advice how to proceed

 <td align="center"><input type="checkbox" align="center" id="all" class="groupCheckBox" name="emails[]" value=' . $user['id'] . '></td>

Below code I used for get the values of checked boxes in all pages on button click

 $('#merge_button').click(function () {   
         var id = ""; 
         var oTable = $("#userTable").dataTable(); 
         $(".groupCheckBox:checked", oTable.fnGetNodes()).each(function () { 

             if (id != "") { 
                 id = id + "," + $(this).val(); 
             } else { 
                 id = $(this).val(); 
             } 

             document.getElementById("email").value = id; 
         }); 
    }); 

but now I want to count the checkedboxes without button click function whenever user clicked on checkboxes and deduct count when unchecking boxes . pls advice

var table = $("#userTable").DataTable();

var countchecked = table
    .rows()
    .nodes()
    .to$()      // Convert to a jQuery object
.find('input[type="checkbox"].groupCheckBox:checked').length;

Uses the most recent API

"...please be aware that using deferRender will cause some nodes to be created only when they are required for display, so they might not be immediately available when this method is called." from https://datatables.net/reference/api/rows().nodes()

很简单。

$("td input[type='checkbox']:checked").length;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM