简体   繁体   English

通过datatable jquery插件添加的按钮上的jQuery click事件

[英]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. 问题:未触发按钮的单击。

Javascript where table is created. 创建表的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>

Javascript where On click is called 称为“点击”的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 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表格

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM