【问题标题】:Select Checkbox !== Select Row Table选择复选框!== 选择行表
【发布时间】:2017-11-28 01:47:27
【问题描述】:

select/unselect 按钮适用于复选框。

但它不适用于行表。

//Select row table
$('#example').on('click', 'tr', function() {
  var $row = $(this),
    isSelected = $row.hasClass('selected')
  $row.toggleClass('selected')
    .find(':checkbox').prop('checked', !isSelected);
});

// Problem : Checkbox !== select row
$("#selectAll, #unselectAll").on("click", function() {
  var selectAll = this.id === 'selectAll';
  $("#example tr :checkbox").prop('checked', selectAll);
});

我认为清单只是为了显示,用于选择行并标记它。

如何单击选择/取消选择按钮时,

它在行上选择 表也​​是,不只是在复选框上?

点击行表可以看到。

代码片段演示:

$('#example').dataTable();

//Select row table
$('#example').on('click', 'tr', function() {
  var $row = $(this),
    isSelected = $row.hasClass('selected')
  $row.toggleClass('selected')
    .find(':checkbox').prop('checked', !isSelected);
});

// Problem : Checkbox !== select row
$("#selectAll, #unselectAll").on("click", function() {
  var selectAll = this.id === 'selectAll';
  $("#example tr :checkbox").prop('checked', selectAll);
});
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<link href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css" rel="stylesheet"/>


<button type="button" id="selectAll"> Select </button>
<button type="button" id="unselectAll"> UnSelect </button>

<table id="example" class="myclass" />
<thead>
  <tr>
    <th>
    </th>
    <th>Name</th>
    <th>Company</th>
    <th>Employee Type</th>
    <th>Address</th>
    <th>Country</th>
  </tr>
</thead>
<tbody>

  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>Calvin</td>
    <td>TCS</td>
    <td>IT</td>
    <td>San Francisco</td>
    <td>US</td>
  </tr>

  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>Ananda</td>
    <td>TCS</td>
    <td>IT</td>
    <td>San Francisco</td>
    <td>US</td>
  </tr>

  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>John</td>
    <td>TCS</td>
    <td>IT</td>
    <td>San Francisco</td>
    <td>US</td>
  </tr>

  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>Doe</td>
    <td>TCS</td>
    <td>IT</td>
    <td>San Francisco</td>
    <td>US</td>
  </tr>
</tbody>

JSFiddle

【问题讨论】:

    标签: javascript jquery checkbox


    【解决方案1】:

    一种方法是在提供second argument 的行上调用.toggleClass(),表示是添加还是删除类:

      $("#example tr").toggleClass("selected", selectAll)
        .find(":checkbox").prop('checked', selectAll);
    

    添加到您的演示中:

    $('#example').dataTable();
    
    //Select row table
    $('#example').on('click', 'tr', function() {
      var $row = $(this),
        isSelected = $row.hasClass('selected')
      $row.toggleClass('selected')
        .find(':checkbox').prop('checked', !isSelected);
    });
    
    // Problem : Checkbox !== select row
    $("#selectAll, #unselectAll").on("click", function() {
      var selectAll = this.id === 'selectAll';
      $("#example tr").toggleClass("selected", selectAll)
        .find(":checkbox").prop('checked', selectAll);
    });
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
    <link href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css" rel="stylesheet"/>
    
    
    <button type="button" id="selectAll"> Select </button>
    <button type="button" id="unselectAll"> UnSelect </button>
    <table id="example" class="myclass" />
    <thead>
      <tr>
        <th></th><th>Name</th><th>Company</th><th>Employee Type</th><th>Address</th><th>Country</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td><input type="checkbox" /></td><td>Calvin</td><td>TCS</td><td>IT</td><td>San Francisco</td><td>US</td>
      </tr>
      <tr>
        <td><input type="checkbox" /></td><td>Ananda</td><td>TCS</td><td>IT</td><td>San Francisco</td><td>US</td>
      </tr>
      <tr>
        <td><input type="checkbox" /></td><td>John</td><td>TCS</td><td>IT</td><td>San Francisco</td><td>US</td>
      </tr>
      <tr>
        <td><input type="checkbox" /></td><td>Doe</td><td>TCS</td><td>IT</td><td>San Francisco</td><td>US</td>
      </tr>
    </tbody>

    【讨论】:

    • 当我想执行所选内容时不起作用。我需要选择一行,这样才能正常工作.. The Problem
    • 对不起,我不明白你的意思。
    • 对不起,我真的很喜欢你的作品。但是您可以看到,在我单击“全选”并将其复制后,它不起作用。在图片中,我需要先选择 1 行,然后单击“全选”,这样复制过程才能运行良好。
    • 什么复制过程?您似乎在询问问题中未显示的功能。另外,我不知道你点击全选后先选择一行是什么意思。
    • 我的意思不是这样,就像'全选'没有完美地选择行。
    【解决方案2】:

    试试这个代码:

    $('#example').dataTable();
       //Select row table
     $('#example').on('click', 'tr', function() {
       var $row = $(this);
       isSelected = $row.hasClass('selected')
       $row.toggleClass('selected').find(':checkbox').prop('checked',!isSelected);
     });
    
     //Checkbox !== select row
     var isSelectAll = false;
     var isUnselectAll = false;
     $("#selectAll, #unselectAll").on("click", function() {
    
       var selectAll = this.id === 'selectAll';
       if(selectAll){
         if(!isSelectAll){
         $('#example tr').click();
       }
         isSelectAll = true;
         isUnselectAll = false;
       }else{
         if(!isUnselectAll){
            $('#example tr').click();
        }
        isSelectAll = false;
        isUnselectAll = true;
      }
    
    
    $("#example tr :checkbox").prop('checked', selectAll);
    
    });
    

    【讨论】:

    • 酷...感谢您的工作。这很有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-30
    • 1970-01-01
    相关资源
    最近更新 更多