启动表-无法在tr

Bootstrap table - cannot add click event on tr

本文关键字:tr 启动      更新时间:2023-09-26

我正在使用Bootstrap表(http://wenzhixin.net.cn/p/bootstrap-table/docs/index.html)

我正在尝试添加点击事件

$('tr').click(function(){ console.log('test'); });

但它不起作用。我知道引导表库中有事件,但对我来说,准确地将其与jQuery的.click一起使用很重要。你知道引导表源代码中阻止这个事件的是什么吗?我试着从bootstrap-table.js中删除所有".off",但无济于事。

我认为您可以使用onClickRowclick-row.bs.table事件来代替tr click事件,下面是文档和示例。

代码示例:

// Fires when user click a row
$('#table').bootstrapTable({
    onClickRow: function (row, $element) {
        // row: the record corresponding to the clicked row, 
        // $element: the tr element.
    }
});
// or
$('#table').on('click-row.bs.table', function (e, row, $element) {
    // console.log(row, $element);
});

(我是Bootstrap Table的作者,希望能帮助你!)

试试这个

 $('table').on('click', 'tr' , function (event) {
    console.log('test');
            });

我尝试过这段代码,成功获得值find td(td:eq(1))。

        $('#tbname').on('change', 'tr' , function (event) {
            if($('.selected')){
                kode =$('.selected').closest('tr').find('td:eq(1)').text();
                $("input").val(kode);
           }

我几天前刚开始使用这个有用的插件,我也遇到了同样的问题,用户wenyi给出了一个很好的答案,但对于刚开始使用的人来说有点复杂。

/* You have this table */
<table id="my-table">
   <tr>
      <a href="#" class="my-link">Details</a>
   </tr>
</table>
/* You have to options to detect this click (and more) */
/* Normal way */
$(".my-link").click(function(){
   //do something
});
/* To solve this click detection problem you will need to use this */
$("#my-table").on('click','.my-link',function(){
   //do something
});

为什么?因为每次在DOM中动态添加一个html元素时,都无法用正常的"点击函数"检测到它们。

附加:

/* Even you can use this method, but the performance is less */
    $(document).on('click','.my-link',function(){
   //do something
});