如何从bootstrap表获取td click事件?

问题描述 投票:1回答:3

我正在尝试将事件属性引用到我的引导表的不同列的单元格。

问题是:虽然我已经为每个列设置了一个类,但是不会触发归于此类的事件。我仍然尝试为在单元格中添加的每个div设置一个类,但它的事件也没有被触发。

那么,如何使用bootstrap表获取td事件?

javascript jquery twitter-bootstrap events bootstrap-table
3个回答
1
投票

以下是bootstrap表的td click事件的完整代码

$(document).ready(function(){
  $('table td').click(function(){
     alert($(this).html());
  });
  
  });
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  
  <p>Click on the td below</p>            
  <table class="table">
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>John</td>
        <td>Doe</td>
        <td>[email protected]</td>
      </tr>
      <tr>
        <td>Mary</td>
        <td>Moe</td>
        <td>[email protected]</td>
      </tr>
      <tr>
        <td>July</td>
        <td>Dooley</td>
        <td>[email protected]</td>
      </tr>
    </tbody>
  </table>
</div>

</body>
</html>

1
投票

在您向我们展示一些代码之前,您可以试试这个,

$('body').on('click', '#tableID tr td', function(){
  //whenever any td is clicked, this function will get called
});

你可以用你要绑定你的td的任何事件名称来改变'click'

我将事件委托给body,因为如果你动态地将你的表元素添加到你的html,直接绑定到那些事件将不会触发/工作。


0
投票

试试这个

$("#boottable").on('all.bs.table', function (e, name, args) {
            console.log(name, args);
        });
© www.soinside.com 2019 - 2024. All rights reserved.