如何获取表中的所有选中列

时间:2018-03-05 12:33:35

标签: jquery html

我需要返回所选cols的数量,这是我的代码:

getSelectedColumns: function() {

  var cols = [];
  $('tbody > tr > th input[type="checkbox"]:checked', table).each(function() {
    cols.push($(this).val());
  });

  return cols;
},
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table table-striped table-bordered table-hover table-checkable" id="datatable_ajax">
  <thead>
    <tr role="row" class="heading">
      <th width="2%">
        <label class="mt-checkbox mt-checkbox-single mt-checkbox-outline">
    	<input type="checkbox" class="group-checkable" data-set="#sample_2 .checkboxes" />
    	<span></span>
    	</label>
      </th>
      <th width="5%"><input type="checkbox" class="group-column-checkable" /> Closing Date </th>

1 个答案:

答案 0 :(得分:0)

&#13;
&#13;
function getSelectedColumns() {
  var cols = [];
  $('thead tr th input[type=checkbox]:checked').each(function() {
    cols.push($(this).val());
  });

  return cols;
}

$("input[type=checkbox]").on("click", function() {
var test = getSelectedColumns();
  console.log(test);
  console.log("Count: " + test.length);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table class="table table-striped table-bordered table-hover table-checkable" id="datatable_ajax">
  <thead>
    <tr role="row" class="heading">
      <th width="2%">
        <label class="mt-checkbox mt-checkbox-single mt-checkbox-outline">
                    <input type="checkbox" class="group-checkable" data-set="#sample_2 .checkboxes" value="100"/>
                    <span></span>
                </label>
      </th>
      <th width="5%"><input type="checkbox" class="group-column-checkable" value="200" /> Closing Date </th>
    </tr>
  </thead>
</table>
&#13;
&#13;
&#13;

相关问题