简体   繁体   English

当按钮:['colvis'] 激活时,复选框单击事件在数据表行中不起作用

[英]Checkbox click event not working in datatable row when buttons: [ 'colvis' ] activeted

I have 1 question here: How to get a Checkbox Value in JavaScript .我在这里有 1 个问题: How to get a Checkbox Value in JavaScript

  • With the help it worked very well.在帮助下,它工作得很好。 However, when I added buttons: ['colvis'] to DataTable, the click checkbox events did not seem to work correctly.但是,当我将按钮:['colvis'] 添加到 DataTable 时,单击复选框事件似乎无法正常工作。 I have looked for help on the DataTable forum, but to no avail.我在 DataTable 论坛上寻求帮助,但无济于事。

Code:代码:

<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);
})

In this case, you may need to update the event onchange for the checkbox #checkedAll .在这种情况下,您可能需要更新复选框#checkedAll onchange事件。

Since you want to use it with jquery, you can remove the attribute onclick="toggle(this)" from由于您想将它与 jquery 一起使用,您可以从中删除属性onclick="toggle(this)"

<input type="checkbox" onclick="toggle(this)" name="checkedAll" id="checkedAll" />

Then, in your js file, you can write the event like this然后,在您的 js 文件中,您可以像这样编写事件

$('#checkedAll').on('change', toggle);

Then, because we use toggle function as the event for this checkbox, we don't need source parameter anymore, just replace it with this :然后,因为我们使用的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.

相关问题 jQuery数据表-操作Colvis按钮的CSS - JQuery Datatable - Manipulating the CSS of the Colvis buttons 复选框单击事件在数据表的第二页上不起作用? - checkbox Click event is not working from second page in datatable? Angular - 数据表点击行事件 - Angular - Datatable click row event 带有行单击事件的复选框标题在 extjs 6.5.1 中无法正常工作 - checkbox header with row click event is not working properly in extjs 6.5.1 标题上的数据表单击事件不起作用 - Datatable click event on header not working 动态创建复选框单击事件选择整行,而不是仅选择jquery数据表中的复选框。 这是为什么? - Dynamically created checkbox click event selecting the whole row instead of only the check box in a jquery datatable. Why is that? 当我单击敲除表行中的复选框时,tr单击事件将覆盖复选框状态更改 - When I click a checkbox in a knockout table row, the tr click event overrides the checkbox state change 当父级侦听单击事件时,复选框在 ngTemplateOutlet 内不起作用 - 错误? - Checkbox not working inside ngTemplateOutlet when parent listens for click event - bug? 点击事件不适用于复选框标签 - Click event not working with the checkbox tag 父项具有点击事件时,复选框功能不起作用 - Checkbox functionality not working when parent has a click event
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM