【问题标题】:How get td click event from bootstrap table?如何从引导表中获取 td 点击事件?
【发布时间】:2015-07-24 18:25:10
【问题描述】:

我正在尝试将事件归因于我的引导表的不同列的单元格。

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

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

【问题讨论】:

  • 请贴出代码你已经做了什么?
  • 你能发布你的代码吗?

标签: javascript jquery twitter-bootstrap events bootstrap-table


【解决方案1】:

这里是引导表的td点击事件的完整代码

$(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>john@example.com</td>
      </tr>
      <tr>
        <td>Mary</td>
        <td>Moe</td>
        <td>mary@example.com</td>
      </tr>
      <tr>
        <td>July</td>
        <td>Dooley</td>
        <td>july@example.com</td>
      </tr>
    </tbody>
  </table>
</div>

</body>
</html>

【讨论】:

    【解决方案2】:

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

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

    您可以将 'click' 更改为您想要绑定 td 的任何事件名称。

    我将事件委托给正文,因为如果您将表格元素动态添加到 html,直接绑定到这些事件将不会触发/起作用。

    【讨论】:

    • Suman 有正确的想法,所以我不打算单独写答案,但即使您进行动态更改,您也可以绑定到比“body”嵌套更深的元素。将侦听器绑定到不会被销毁的最近的祖先。 $('#someContainer') 或其他。然后在选择器中,不需要指定tr,但这是个人选择。
    • 我的计划是编辑/更改“body”到最接近的父元素,一旦 Doglas 发布他的一些代码:) 支持您的评论@GregPettit
    【解决方案3】:

    试试这个

    $("#boottable").on('all.bs.table', function (e, name, args) {
                console.log(name, args);
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多