【发布时间】:2021-05-24 07:48:56
【问题描述】:
我已经创建了 jQuery DataTables 复选框,现在我的问题是我无法在数据表中显示多个复选框以进行选择。我试图为 jQuery DataTables 库添加一个 Checkboxes 扩展,它提供了一个通用的解决方案来处理表中的复选框。但是错误告诉我消息是Script error希望有人能指出我遇到了哪一个错误。谢谢。
以下是我的示例代码:
$(document).ready(function() {
var table = $('#example').DataTable({
'columnDefs': [
{
'targets': 0,
'checkboxes': {
'selectRow': true
}
}
],
'select': {
'style': 'multi'
},
'order': [[1, 'asc']]
});
// Handle form submission event
$('#frm-example').on('submit', function(e){
var form = this;
var rows_selected = table.column(0).checkboxes.selected();
// Iterate over all selected checkboxes
$.each(rows_selected, function(index, rowId){
// Create a hidden element
$(form).append(
$('<input>')
.attr('type', 'hidden')
.attr('name', 'id[]')
.val(rowId)
);
});
// Output form data to a console
$('#example-console-rows').text(rows_selected.join(","));
// Output form data to a console
$('#example-console-form').text($(form).serialize());
// Remove added elements
$('input[name="id\[\]"]', form).remove();
// Prevent actual form submission
e.preventDefault();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.21/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.24/css/jquery.dataTables.min.css" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-datatables-checkboxes@1.2.12/css/dataTables.checkboxes.css" />
<script src="https://cdn.jsdelivr.net/npm/jquery-datatables-checkboxes@1.2.12/js/dataTables.checkboxes.min.js"></script>
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Extn</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Name</td>
<td>Position</td>
<td>Office</td>
<td>Extn</td>
<td>Start date</td>
<td>Salary</td>
</tr>
<tr>
<td>2</td>
<td>Name</td>
<td>Position</td>
<td>Office</td>
<td>Extn</td>
<td>Start date</td>
<td>Salary</td>
</tr>
<tr>
<td>3</td>
<td>Name</td>
<td>Position</td>
<td>Office</td>
<td>Extn</td>
<td>Start date</td>
<td>Salary</td>
</tr>
</tbody>
<tfoot>
<tr>
<th></th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
</table>
这是我的预期结果:
【问题讨论】:
-
您的 sn-p 缺少 jquery。 @ 987654322@ sn-p 并从左侧的下拉菜单中选择 jquery 然后运行您的 sn-p 并且它工作正常 - 我没有为您进行此更改,因为这是您遇到的唯一问题,因为它会解决它在问题中。
标签: jquery datatable jquery-datatables-checkboxes