How add more then one button in each row in JQuery

2019-05-24 07:06发布

I have one button but I need two buttons and perform some MySql when they are clicked to get data from the underlying database.

How can I perform an event using these two buttons?

$(document).ready(function () {
    var table= $('#example').dataTable( {
        "ajax": "..//wp-content/plugins/jobify/Admin/data.txt",
        "columnDefs": [ {
            "targets": -1,
            "data": null,
            "defaultContent": "<button>View Posted Jobs</button>"
        } ]
    } );

    $('#example tbody').on( 'click', 'button', function () {
        //var data = table.row( $(this).parents('tr') ).data();
    } );
} );

1条回答
Fickle 薄情
2楼-- · 2019-05-24 08:02

Just assign a class to each button and attach an event handler to each class.

$(document).ready(function () {
   var table = $('#example').dataTable( {
      "ajax": "../wp-content/plugins/jobify/Admin/data.txt",
       "columnDefs": [ {
          "targets": -1,
          "data": null,
          "defaultContent": 
             '<button class="btn-view" type="button">View Posted Jobs</button>'
             + '<button class="btn-delete"  type="button">Delete</button>'
        } ]
     } );

     // Handle click on "View" button
     $('#example tbody').on('click', '.btn-view', function (e) {
        //var data = table.row( $(this).parents('tr') ).data();
     } );


     // Handle click on "Delete" button
     $('#example tbody').on('click', '.btn-delete', function (e) {
        //var data = table.row( $(this).parents('tr') ).data();
     } );
});
查看更多
登录 后发表回答