gpt4 book ai didi

javascript - 通过数据表 jquery 插件添加的按钮上的 jQuery 单击事件

转载 作者:行者123 更新时间:2023-11-28 17:42:42 26 4
gpt4 key购买 nike

我正在使用数据表插件创建一个表。已将按钮添加到表格的最后一列。问题:单击按钮时未触发。

创建表的 JavaScript。

<script>
$(document).ready(function() {
$('#tagtbl').DataTable( {
"paging": false,
"info": false,
"searching": false,
"ajax": "../api/clienttagtbl.php?off=OFF002",
"rowId": "id",
"columns": [
{ "data": "tagname" },
{ "data": null, "defaultContent": '<button id="tagdelete" type="button" class="btn btn-danger btn-sm" >X</button>' }
]
} );
} );
</script>

调用 On click 的 JavaScript

<script>
$(document).ready(function() {
$('#tagdelete').click( function(){
console.log("hi"); // <<---This is not working
});
$('#tagtbl').click( function(){
console.log("hi2"); //<<---This is working
});
});
</script>

HTML

    <table id="tagtbl" class="table table-sm" >
<thead>
<tr>
<th>Tag Name</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>Tag Name</th>
<th></th>
</tr>
</tbody>
</table>

HTML Table

最佳答案

试试这个

 $('#tagtbl').on('click','.tagdelete' function(){
console.log("hi");
});

我建议您对所有按钮使用一个类,而不是使用 id。

关于javascript - 通过数据表 jquery 插件添加的按钮上的 jQuery 单击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47566746/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com