1

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

  • 在帮助下,它工作得很好。但是,当我向 DataTable 添加按钮:['colvis'] 时,单击复选框事件似乎无法正常工作。我在 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);
})
4

1 回答 1

2

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

由于您想将它与 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>

于 2020-01-09T05:20:16.343 回答