【问题标题】:How to count checkboxes has checked in DataTable for show and hidden Button如何计算复选框已在 DataTable 中检查显示和隐藏按钮
【发布时间】:2020-03-23 21:18:38
【问题描述】:

我使用datatable进行数据收集,我使用datatable中的checkbox功能以删除和编辑按钮的形式选择需要执行的数据,我的删除和编辑按钮会在checkbox的时候一起出现在 1 个数据中被选中,对于选中的复选框超过 1 个按钮将出现,只需将其删除。我这里有个问题是按钮不想出现,按照我的代码

表格和按钮代码:

<div class="col s12 m12 l12">
  <button id="hapus" class="waves-effect waves-light btn" style="display: none">Hapus</button>
  <button id="status" class="waves-effect waves-light btn" style="display: none">Ubah Status</button>

 <table id="example" style="width:100%">
    <thead>
      <tr>
        <th>No</th>
        <th>Judul</th>
        <th>Tag</th>
        <th>Status</th>
        <th>Tanggal</th>
        <th></th>
        </tr>
    </thead>
 </table>

jquery:

$(document).ready(function(){
        var xx = document.getElementById("status");
        var xy = document.getElementById("hapus");

        var $checkboxes = $('#example td input[type="checkbox"]');

        $checkboxes.change(function(){
            var countCheckedCheckboxes = $checkboxes.filter(':checked').length;

            if (countCheckedCheckboxes == 1){
                xx.style.display = "block";
                xy.style.display = "block";
            } else if (countCheckedCheckboxes >= 2) {
                xx.style.display = "none";
                xy.style.display = "block";
            } else {
                xx.style.display = "none";
                xy.style.display = "none";
            }
        }); 
    });

【问题讨论】:

    标签: javascript jquery datatable datatables


    【解决方案1】:

    我认为您需要在更改方法中再次获取并计算复选框。您不能重用变量 $checkboxes

    【讨论】:

    • 我在普通表中尝试过这段代码,结果可以工作,但是安装数据表后它不起作用
    • 您是否检查过单击复选框时是否触发了事件?
    • 这段代码是什么意思? var countCheckedCheckboxes = $checkboxes.filter(':checked').length;
    • 我会发出警报(countCheckedCheckboxes);只是为了确保它确实有效并检查该功能是否被触发
    • 我在console.log()上试过了,但没有显示任何东西
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-04
    • 1970-01-01
    • 2016-03-13
    • 2016-04-17
    相关资源
    最近更新 更多