[英]jQuery click event on button added through datatable jquery plugin
I am creating a table using the datatable plugin. 我正在使用数据表插件创建表。 Have added buttons to last column of the table.
在表的最后一列添加了按钮。 Issue: The button's on click is not triggered.
问题:未触发按钮的单击。
<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>
<script>
$(document).ready(function() {
$('#tagdelete').click( function(){
console.log("hi"); // <<---This is not working
});
$('#tagtbl').click( function(){
console.log("hi2"); //<<---This is working
});
});
</script>
<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>
Try this 尝试这个
$('#tagtbl').on('click','.tagdelete' function(){
console.log("hi");
});
i recommend you use a class for all the buttons instead of using id. 我建议您对所有按钮使用一个类,而不要使用id。
you need to add the button click in datatable drawCallback function 您需要在数据表中添加按钮clickCallCallback函数
$('#example').dataTable( {
"drawCallback": function( settings ) {
$('#tagdelete').click( function(){
console.log("hi");
});
$('#tagtbl').click( function(){
console.log("hi2");
});
}
} );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.