javascript 如何在 JQuery 数据表的每一行中添加多个按钮以及如何对它们应用事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31327933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How add more then one button in each row in JQuery datatables and how to apply event on them
提问by Arslan Sheerazi
I have one button but I need two buttons and perform some MySql when they are clicked to get data from the underlying database.
我有一个按钮,但我需要两个按钮并在单击它们以从底层数据库获取数据时执行一些 MySql。
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();
} );
} );
回答by Gyrocode.com
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();
} );
});