javascript - 使用 jquery 在 bootstrap 中获取选定表行的值

标签 javascript jquery twitter-bootstrap bootstrap-table

我正在使用 Bootstrap 表。因为我想在单击同一页面上的 “添加到购物车” 按钮后获取所选表格行的 Item ID 值。

表格代码:

<table data-toggle="table" id="table-style" data-row-style="rowStyle" data-url="tables/data2.json"  data-show-refresh="true" data-show-toggle="true" data-show-columns="true" data-search="true" data-select-item-name="toolbar1" data-pagination="true" data-sort-name="name" data-sort-order="desc" data-single-select="false" data-click-to-select="true" data-maintain-selected="true">
  <thead>
    <tr>
      <th data-field="state" data-checkbox="true"></th>
      <th data-field="id" >Item ID</th>
      <th data-field="name" data-sortable="true">Product Name</th>
      <th data-field="price" data-sortable="true">Actual Price</th>
      <th data-field="discount_price" data-sortable="true">Discount Price</th>
      <th data-field="stock_avail" data-sortable="true">Stock Available</th>
    </tr>
  </thead>
</table>

JQuery 代码:

$(document).ready(function()
{
   $("#add_cart").click(function()
   {
      //foreach selected row retrieve 'Item ID' values in array;
      //call ajax for otherpage.php?arr='Item ID array';
   });
});

因为我是 bootstrap 的新手,所以我正在尝试解决这个问题,但没有得到适当的解决方案任何人请告诉我这个。

enter image description here

最佳答案

只需使用 check.bs.tableuncheck.bs.table 事件来收集您选中的行。

BS-Table Basic Events

这是一个例子:

var checkedRows = [];

$('#eventsTable').on('check.bs.table', function (e, row) {
  checkedRows.push({id: row.id, name: row.name, forks: row.forks});
  console.log(checkedRows);
});

$('#eventsTable').on('uncheck.bs.table', function (e, row) {
  $.each(checkedRows, function(index, value) {
    if (value.id === row.id) {
      checkedRows.splice(index,1);
    }
  });
  console.log(checkedRows);
});

$("#add_cart").click(function() {
  $("#output").empty();
  $.each(checkedRows, function(index, value) {
    $('#output').append($('<li></li>').text(value.id + " | " + value.name + " | " + value.forks));
  });
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.8.1/bootstrap-table.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.8.1/bootstrap-table.min.js"></script>

<table id="eventsTable"
       data-toggle="table"
       data-height="300"
       data-url="https://api.github.com/users/wenzhixin/repos?type=owner&sort=full_name&direction=asc&per_page=100&page=1"
       data-pagination="true"
       data-search="true"
       data-show-refresh="true"
       data-show-toggle="true"
       data-show-columns="true"
       data-toolbar="#toolbar">
    <thead>
    <tr>
        <th data-field="state" data-checkbox="true"></th>
        <th data-field="name">Name</th>
        <th data-field="stargazers_count">Stars</th>
        <th data-field="forks_count">Forks</th>
        <th data-field="description">Description</th>
    </tr>
    </thead>
</table>

<button id="add_cart">Add to card</button>
<ul id="output"></ul>

关于javascript - 使用 jquery 在 bootstrap 中获取选定表行的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32134884/

相关文章:

css - 在 Bootstrap 中完全居中表单

javascript - RequireJs 没有正确加载依赖项

javascript - 在 Google Fusion map 中使用多个下拉菜单过滤结果

javascript - 为什么我的翻译动画不起作用?

javascript - 动态更改 Twitter Bootstrap 模式的文本

jquery - 在 c3 圆环图中设置起点

javascript - 检查元素是否具有 CSS 类。如果没有添加的话

javascript - 覆盖排序索引

javascript - moment 在 Internet Explorer 和 Safari 上返回无效日期

javascript - 使用 CloudFlare 背后的 NodeJS 跟踪页面查看时间