Senario: So basically I\'m using DataTables and have checkboxes on its first column. My DataTables have multiple pages (pagination).
The Pro
jQuery DataTables removes non-visible rows from DOM for performance reasons, that is why when you submit the form only visible checkboxes get submitted.
You may need to turn those that are checked and don't exist in DOM into
upon form submission.
For example, to submit form with values of all checkboxes:
var table = $('#example').DataTable();
$("form").on('submit', function(e){
var $form = $(this);
// Iterate over all checkboxes in the table
table.$('input[type="checkbox"]').each(function(){
// If checkbox doesn't exist in DOM
if(!$.contains(document, this)){
// If checkbox is checked
if(this.checked){
// Create a hidden element
$form.append(
$('')
.attr('type', 'hidden')
.attr('name', this.name)
.val(this.value)
);
}
}
});
});
If you're submitting the form via Ajax, it's even simpler.
For example, to submit form via Ajax with values of all checkboxes:
var table = $('#example').DataTable();
$("#btn-submit").on('click', function(e){
e.preventDefault();
$.ajax({
url: "/path/to/your/script.php",
data: table.$('input[type="checkbox"]').serialize();
}).done(function(data){
console.log("Response", data);
});
});
See our article jQuery DataTables: How to submit all pages form data for demonstration.