Javascript jQuery数据表获取<;tr>;点击时识别
我有一个带有静态列的datatable,该列中填充了一个delete按钮,因此用户可以删除表中的条目。但是,我试图弄清楚如何在单击链接时获取行的“id”属性,以便在发送删除请求时将其作为参数包含 代码:Javascript jQuery数据表获取<;tr>;点击时识别,javascript,jquery,onclick,datatables,Javascript,Jquery,Onclick,Datatables,我有一个带有静态列的datatable,该列中填充了一个delete按钮,因此用户可以删除表中的条目。但是,我试图弄清楚如何在单击链接时获取行的“id”属性,以便在发送删除请求时将其作为参数包含 代码: //设置我们的数据表 var table=jQuery(“#服务_记录”)。数据表({ “数据”:t.responseJSON, “分页”:正确, “排序”:正确, “订单”:[[0,“说明”]], “信息”:错误, “列”:列, “columnDefs”:[{ “目标”:-1, “数据”:空,
//设置我们的数据表
var table=jQuery(“#服务_记录”)。数据表({
“数据”:t.responseJSON,
“分页”:正确,
“排序”:正确,
“订单”:[[0,“说明”]],
“信息”:错误,
“列”:列,
“columnDefs”:[{
“目标”:-1,
“数据”:空,
“defaultContent”:”
} ],
“fnRowCallback”:函数(nRow、aData、iDisplayIndex、iDisplayIndexFull){
jQuery(nRow).attr('id',aData['primary_id_field']);
返回nRow;
}
});
//添加侦听器以响应删除单击
jQuery(“#服务_记录”)。在('click','delete',函数(e)上{
var id=jQuery(this.attr('id');
//返回
返回false;
});
aData[]
是一个数组,需要的是整数,而不是字符串
因此,您需要做的不是
aData['primary\u id\u field']
而是aData[0]
或主id列的索引。因此,如果id位于第一列,它将是0
,等等。在onclick事件中,只需使用this.id
使用上述方法时没有值。可能是jQuery(此).最近的('tr').attr('id')代码>这成功了!谢谢。“id”属性不是这样设置的。
// Set up our datatable
var table = jQuery('#service_record').dataTable( {
"data": t.responseJSON,
"paging": true,
"ordering": true,
"order": [[ 0, "desc" ]],
"info": false,
"columns": columns,
"columnDefs": [ {
"targets": -1,
"data": null,
"defaultContent": "<u><a href='#'>Delete</a></u>"
} ],
"fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull) {
jQuery(nRow).attr('id', aData['primary_id_field']);
return nRow;
}
});
// Add a listener to respond to the deletion click
jQuery('#service_record').on('click','.delete', function (e) {
var id = jQuery(this).attr('id');
// Return
return false;
});