我正在使用数据表插件创建一个表。已将按钮添加到表格的最后一列。问题:单击按钮时未触发。
创建表的 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>
最佳答案
试试这个
$('#tagtbl').on('click','.tagdelete' function(){
console.log("hi");
});
我建议您对所有按钮使用一个类,而不是使用 id。
关于javascript - 通过数据表 jquery 插件添加的按钮上的 jQuery 单击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47566746/