在jquery中获取所选tr的值

时间:2017-05-26 13:56:30

标签: javascript jquery html css datatable

我有一个表,它包含每行复选框的数据。选中任何复选框后,我会将Selected类添加到相应的tr元素中。

$('#InventoryTable tbody').on('click', 'input[type="checkbox"]', function (e) { /** logic */ });

但是,当点击按钮时获取所选的tr元素时,我面临以下问题。

这是我用来获取seletced tr值的代码:

function myFunc() {
    debugger;

    var table = $('#InventoryTable').DataTable();
    var adjustmentdetails = new Array();
    $("#InventoryTable tr.selected").each(function (ix, element) {

        // Do your processing.
         debugger;
    });
}

但是,我无法使用上面的代码获取其他页面(分页)选定的值。

2 个答案:

答案 0 :(得分:0)

只需将此代码转换为函数

function clickcheck(e){
  { //logic });
}

并将此功能应用于您的复选框onclick attr并在此功能上传递事件

答案 1 :(得分:0)

这是一种方法,我假设添加类函数适合你,然后使用一个按钮点击get all element with type = checkbox,它是带有类selected的输入元素。然后打印这个值(把你的逻辑放在这里)

$('input[type=checkbox]').on('click', function(e) {
  if (this.checked) {
    $(this).addClass('selected');
  } else {
    $(this).removeClass('selected');
  }
});

$('#getAllCheckedValue').on('click', function() {
  console.log('----------- getAllCheckedValue ----------------');
  $("input.selected[type=checkbox]").each(function(ix, element) {
    console.log('Checked-->' + $(this).val());
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="InventoryTable">
  <tbody>
    <tr>
      1<input type="checkbox" value="1" class="test test2">
    </tr>
    <tr>
      2<input type="checkbox" value="2">
    </tr>
    <tr>
      3<input type="checkbox" value="3">
    </tr>
  </tbody>
</table>
<button id="getAllCheckedValue">
  Get All Checked Value
</button>

相关问题