[英]Checkbox click event not working in datatable row when buttons: [ 'colvis' ] activeted
我在這里有 1 個問題: How to get a Checkbox Value in JavaScript 。
代碼:
<input id="listvalue" name="selectedCB">
<table id="datatest" class="table table-striped table-bordered nowrap" style="width:100%">
<thead>
<tr>
<th class="no-sort checkboxor">
<input type="checkbox" onclick="toggle(this)" name="checkedAll" id="checkedAll" /> #
</th>
<th class="no-sort">IDtest</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tycheck">
<input type="checkbox" name="checkAll" value="2" class="checkSingle" />
</td>
<td>1</td>
</tr>
<tr>
<td class="tycheck">
<input type="checkbox" name="checkAll" value="1" class="checkSingle" />
</td>
<td>2</td>
</tr>
</tbody>
</table>
$(document).ready(function () {
$('#datatest').DataTable({
buttons: [
'colvis'
]
});
});
var idsArr = [];
var displayField = $('input[name=selectedCB]');
var checkboxes = Array.from($(".tycheck input[type=checkbox]"));
function toggle(source) {
var values = checkboxes.map(x => {
x.checked = source.checked;
return source.checked ? x.value : '';
}).join(source.checked ? ',' : '');
displayField.val(values);
}
function printChecked() {
var values = checkboxes.filter(x => x.checked).map(x => x.value);
displayField.val(values);
}
$.each(checkboxes, function () {
$(this).change(printChecked);
})
在這種情況下,您可能需要更新復選框#checkedAll
onchange
事件。
由於您想將它與 jquery 一起使用,您可以從中刪除屬性onclick="toggle(this)"
<input type="checkbox" onclick="toggle(this)" name="checkedAll" id="checkedAll" />
然后,在您的 js 文件中,您可以像這樣編寫事件
$('#checkedAll').on('change', toggle);
然后,因為我們使用的toggle
功能作為此復選框的情況下,我們並不需要source
參數了,只是將其替換為this
:
function toggle() {
var values = checkboxes.map(x => {
x.checked = this.checked;
return this.checked ? x.value : '';
}).join(this.checked ? ',' : '');
displayField.val(values);
}
$(document).ready(function () { $('#datatest').DataTable({ buttons: [ 'colvis' ] }); }); var idsArr = []; var displayField = $('input[name=selectedCB]'); var checkboxes = Array.from($(".tycheck input[type=checkbox]")); function toggle() { var values = checkboxes.map(x => { x.checked = this.checked; return this.checked ? x.value : ''; }).join(this.checked ? ',' : ''); displayField.val(values); } function printChecked() { var values = checkboxes.filter(x => x.checked).map(x => x.value); displayField.val(values); } $.each(checkboxes, function () { $(this).on('change', printChecked); }); $('#checkedAll').on('change', toggle);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> <script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script> <input id="listvalue" name="selectedCB"> <table id="datatest" class="table table-striped table-bordered nowrap" style="width:100%"> <thead> <tr> <th class="no-sort checkboxor"> <input type="checkbox" name="checkedAll" id="checkedAll" /> # </th> <th class="no-sort">IDtest</th> </tr> </thead> <tbody> <tr> <td class="tycheck"> <input type="checkbox" name="checkAll" value="2" class="checkSingle" /> </td> <td>1</td> </tr> <tr> <td class="tycheck"> <input type="checkbox" name="checkAll" value="1" class="checkSingle" /> </td> <td>2</td> </tr> </tbody> </table>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.