Ana*_*nda 1 javascript checkbox jquery
该select/unselect按钮适用于复选框.
但它不适用于行表.
//Select row table
$('#example').on('click', 'tr', function() {
var $row = $(this),
isSelected = $row.hasClass('selected')
$row.toggleClass('selected')
.find(':checkbox').prop('checked', !isSelected);
});
// Problem : Checkbox !== select row
$("#selectAll, #unselectAll").on("click", function() {
var selectAll = this.id === 'selectAll';
$("#example tr :checkbox").prop('checked', selectAll);
});
Run Code Online (Sandbox Code Playgroud)
我认为清单只是用于显示,选择行并标记它.
单击选择/取消选择按钮时的方式,
它也选择在行表上,而不仅仅是在复选框上?
您可以查看是否单击了行表.
代码片段演示:
$('#example').dataTable();
//Select row table
$('#example').on('click', 'tr', function() {
var $row = $(this),
isSelected = $row.hasClass('selected')
$row.toggleClass('selected')
.find(':checkbox').prop('checked', !isSelected);
});
// Problem : Checkbox !== select row
$("#selectAll, #unselectAll").on("click", function() {
var selectAll = this.id === 'selectAll';
$("#example tr :checkbox").prop('checked', selectAll);
});Run Code Online (Sandbox Code Playgroud)
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<link href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css" rel="stylesheet"/>
<button type="button" id="selectAll"> Select </button>
<button type="button" id="unselectAll"> UnSelect </button>
<table id="example" class="myclass" />
<thead>
<tr>
<th>
</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>Calvin</td>
<td>TCS</td>
<td>IT</td>
<td>San Francisco</td>
<td>US</td>
</tr>
<tr>
<td>
<input type="checkbox" />
</td>
<td>Ananda</td>
<td>TCS</td>
<td>IT</td>
<td>San Francisco</td>
<td>US</td>
</tr>
<tr>
<td>
<input type="checkbox" />
</td>
<td>John</td>
<td>TCS</td>
<td>IT</td>
<td>San Francisco</td>
<td>US</td>
</tr>
<tr>
<td>
<input type="checkbox" />
</td>
<td>Doe</td>
<td>TCS</td>
<td>IT</td>
<td>San Francisco</td>
<td>US</td>
</tr>
</tbody>Run Code Online (Sandbox Code Playgroud)
一种方法是调用.toggleClass()提供第二个参数的行,该参数表示是否添加或删除该类:
$("#example tr").toggleClass("selected", selectAll)
.find(":checkbox").prop('checked', selectAll);
Run Code Online (Sandbox Code Playgroud)
添加到您的演示中:
$('#example').dataTable();
//Select row table
$('#example').on('click', 'tr', function() {
var $row = $(this),
isSelected = $row.hasClass('selected')
$row.toggleClass('selected')
.find(':checkbox').prop('checked', !isSelected);
});
// Problem : Checkbox !== select row
$("#selectAll, #unselectAll").on("click", function() {
var selectAll = this.id === 'selectAll';
$("#example tr").toggleClass("selected", selectAll)
.find(":checkbox").prop('checked', selectAll);
});Run Code Online (Sandbox Code Playgroud)
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<link href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css" rel="stylesheet"/>
<button type="button" id="selectAll"> Select </button>
<button type="button" id="unselectAll"> UnSelect </button>
<table id="example" class="myclass" />
<thead>
<tr>
<th></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>Calvin</td><td>TCS</td><td>IT</td><td>San Francisco</td><td>US</td>
</tr>
<tr>
<td><input type="checkbox" /></td><td>Ananda</td><td>TCS</td><td>IT</td><td>San Francisco</td><td>US</td>
</tr>
<tr>
<td><input type="checkbox" /></td><td>John</td><td>TCS</td><td>IT</td><td>San Francisco</td><td>US</td>
</tr>
<tr>
<td><input type="checkbox" /></td><td>Doe</td><td>TCS</td><td>IT</td><td>San Francisco</td><td>US</td>
</tr>
</tbody>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
801 次 |
| 最近记录: |