CAUSE
出于性能原因,jQuery DataTables 从 DOM 中删除不可见的行,这就是为什么当您提交表单时仅提交可见的复选框。
SOLUTION
您可能需要将这些<input type="checkbox">
已检查且不存在于 DOM 中<input type="hidden">
提交表格后。
例如,要提交包含所有复选框值的表单:
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(
$('<input>')
.attr('type', 'hidden')
.attr('name', this.name)
.val(this.value)
);
}
}
});
});
如果您通过 Ajax 提交表单,那就更简单了。
例如,要通过 Ajax 提交包含所有复选框值的表单:
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);
});
});
DEMO
请参阅我们的文章jQuery DataTables:如何提交所有页面表单数据 https://www.gyrocode.com/articles/jquery-datatables-how-to-submit-all-pages-form-data/用于演示。