简体   繁体   中英

Get value of all checkbox selected in jQuery Datatables

I have the next Datatable

HTML

<table id="example" class="row-border hover">
  <thead>
    <tr>
      <th>ID</th>
      <th>Iden.</th>
      <th>Nombres</th>
      <th>Sel.</th>
    </tr>
  </thead>
  <tbody></tbody>
  <tfoot>
    <tr>
      <th>ID</th>
      <th>Iden.</th>
      <th>Nombres</th>
      <th>Sel.</th>
    </tr>
  </tfoot>
</table>

JS

$('#example').DataTable({
    scrollX: true,
    deferRender: true,
    "aaData": data.Data,
    "columns": [{ data: "ID" },
                { data: "Identificacion" },
                { data: "Nombres" },
                { "data"      : null,
                  "targets"   : -1,
                  "orderable" : false,
                  "className" : "all",
                  "width"     : "20px",
                  "render"    : function(data, type, full) {
                                  let texto = "";
                                  texto = '<input type="checkbox" id="check_test" value="' + data.ID + '" />';
                                  return texto;
                              }
                }
               ]
});

I want get value of the selected checkboxes, so i tried this

var aux = [];

aux = $('#example tbody input[type=checkbox]:checked').map(function(_, el) {
        return $(el).val();
      }).get();

however, this only obtains the values selected from the active page of the pagination datatable

How can I get the value of the selected checkboxes on all pages of datable?

Use $() DataTables API method to retrieve data from all pages, not just first page.

For example:

var $checkboxes = $('#example').DataTable().$('input[type=checkbox]:checked');

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