Javascript Bootstrap 表 - 无法在 tr 上添加点击事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26567867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bootstrap table - cannot add click event on tr
提问by user3550394
I'm using Bootstrap table (http://wenzhixin.net.cn/p/bootstrap-table/docs/index.html)
我正在使用 Bootstrap 表(http://wenzhixin.net.cn/p/bootstrap-table/docs/index.html)
I'm trying to add click event
我正在尝试添加点击事件
$('tr').click(function(){ console.log('test'); });
but it doesn't work. I know that there is event in bootstrap-table library, but it's important for me to use it exactly with jQuery's .click. Do you know what is blocking this event in bootstrap-table source code? I tryied to remove all ".off"s from bootstrap-table.js, but it didn't help.
但它不起作用。我知道 bootstrap-table 库中有事件,但对我来说将它与 jQuery 的 .click 完全结合使用很重要。你知道是什么在引导表源代码中阻止了这个事件吗?我尝试从 bootstrap-table.js 中删除所有“.off”,但没有帮助。
回答by wenyi
I think you can use the onClickRowor click-row.bs.tableevent instead of the tr click event, here is the documentationand examples.
我认为您可以使用onClickRoworclick-row.bs.table事件代替 tr click 事件,这里是文档和示例。
Code example:
代码示例:
// 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);
});
(I am the author of Bootstrap Table, hope to help you!)
(我是Bootstrap Table的作者,希望能帮到你!)
回答by Jelle Keizer
Try this
尝试这个
$('table').on('click', 'tr' , function (event) {
console.log('test');
});
回答by Sumari Marco
i have try with this code, success for get value find td(td:eq(1)).
我已尝试使用此代码,成功获取值找到 td(td:eq(1))。
$('#tbname').on('change', 'tr' , function (event) {
if($('.selected')){
kode =$('.selected').closest('tr').find('td:eq(1)').text();
$("input").val(kode);
}
回答by Ernesto Hdez
I just some days ago started using this helpful plugin, I was facing the same problem and the user wenyi gave a good answer but kinda complex for people that are starting.
我几天前才开始使用这个有用的插件,我遇到了同样的问题,用户 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
});
Why? because each time that one html element is added dynamically in the DOM they cannot be detected with the normal 'click function'.
为什么?因为每次在 DOM 中动态添加一个 html 元素时,它们都无法用普通的“点击功能”检测到。
Additonally:
另外:
/* Even you can use this method, but the performance is less */
$(document).on('click','.my-link',function(){
//do something
});

