当按钮[[colvis]]激活时,复选框单击事件在数据表行中不起作用 [英] Checkbox click event not working in datatable row when buttons: [ 'colvis' ] activeted

查看:71
本文介绍了当按钮[[colvis]]激活时,复选框单击事件在数据表行中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有1个问题:如何获取复选框JavaScript中的值

I have 1 question here: How to get a Checkbox Value in JavaScript.


  • 在帮助下,效果很好。但是,当我向DataTable添加按钮:['colvis']时,click复选框事件似乎无法正常工作。我一直在DataTable论坛上寻求帮助,但无济于事。

代码:

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


推荐答案

在这种情况下,您可能需要更新事件 onchange 为复选框 #checkedAll

In this case, you may need to update the event onchange for the checkbox #checkedAll.

由于要与jquery一起使用,因此可以删除属性 onclick = toggle(this) 来自

Since you want to use it with jquery, you can remove the attribute onclick="toggle(this)" from

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

然后,在您的js文件中,您可以这样编写事件

Then, in your js file, you can write the event like this

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

然后,因为我们使用 toggle 函数该复选框的事件,我们不再需要 source 参数,只需将其替换为 this

Then, because we use toggle function as the event for this checkbox, we don't need source parameter anymore, just replace it with 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>

这篇关于当按钮[[colvis]]激活时,复选框单击事件在数据表行中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆