【发布时间】:2015-07-05 22:57:43
【问题描述】:
我的 HTML 页面有多个复选框,可以单独检查它们。我有“全选”按钮,当我点击这个按钮时,所有的复选框都应该被选中,当我再次点击同一个按钮时,所有的复选框都应该被取消选择从所有页面。
在我的原始程序中有数千条记录,但一次显示 10 条记录,但是当用户单击选择时,它应该选择所有数千条记录。
我使用jQuery Datatables plug-in 来显示数据。它提供分页、搜索、排序等功能,因此我一次只在当前页面上显示 10 条记录。如果我单击 Bootstrap Datatable 提供的下一页或页码,将显示另外 10 条记录。正如问题中提到的,我想从所有页面中选择所有复选框。
$(document).ready(function () {
$('body').on('click', '#selectAll', function () {
if ($(this).hasClass('allChecked')) {
$('input[type="checkbox"]', '#example').prop('checked', false);
} else {
$('input[type="checkbox"]', '#example').prop('checked', true);
}
$(this).toggleClass('allChecked');
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>monitoring</title>
<script src="jquery.js"></script>
</head>
<body>
<table id="example" class="myclass">
<thead>
<tr>
<th>
<button type="button" id="selectAll" class="main">
<span class="sub"></span> Select </button></th>
<th>Name</th>
<th>Company</th>
<th>Employee Type</th>
<th>Address</th>
<th>Country</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox"/>
</td>
<td>varun</td>
<td>TCS</td>
<td>IT</td>
<td>San Francisco</td>
<td>US</td>
</tr>
<tr>
<td><input type="checkbox"/>
</td>
<td>Rahuk</td>
<td>TCS</td>
<td>IT</td>
<td>San Francisco</td>
<td>US</td>
</tr>
<tr>
<td><input type="checkbox"/>
</td>
<td>johm Doe</td>
<td>TCS</td>
<td>IT</td>
<td>San Francisco</td>
<td>US</td>
</tr>
<tr>
<td><input type="checkbox"/>
</td>
<td>Sam</td>
<td>TCS</td>
<td>IT</td>
<td>San Francisco</td>
<td>US</td>
</tr>
<tr>
<td><input type="checkbox"/>
</td>
<td>Lara</td>
<td>TCS</td>
<td>IT</td>
<td>San Francisco</td>
<td>US</td>
</tr>
<tr>
<td><input type="checkbox"/>
</td>
<td>Jay</td>
<td>TCS</td>
<td>IT</td>
<td>San Francisco</td>
<td>US</td>
</tr>
<tr>
<td><input type="checkbox"/>
</td>
<td>Tom</td>
<td>TCS</td>
<td>IT</td>
<td>San Francisco</td>
<td>US</td>
</tr>
</tbody>
</table>
</body>
</html>
【问题讨论】:
-
table标签不是自闭合标签,请更改<table id="example" class="myclass"/>并删除/ -
你发布了同样的问题stackoverflow.com/questions/29841547/… 和stackoverflow.com/questions/29823843/…? 在相同的问题上使用编辑链接
-
您的代码应该可以工作。您能否分享您遇到的错误或您面临的问题?您为什么再次发布相同的问题,请编辑您之前的问题或告诉我们您面临的实际问题。
-
@varun。 所有页面是什么意思,你的完整表格不是一页吗?您如何在不同的页面中显示您的表格?请在您的问题中添加此信息,以便此处的 ppl 可以为您提供更好的帮助。
标签: javascript jquery html jquery-datatables