如何从数据table中获取所选table单元格的值?

How to get values of the selected table cell from datatable?

这是我的 Jquery:

var oTable_salary = $('#jsontable_salary').dataTable();  //Initialize the datatable
$("#btn_ca_salary").click(function(){
    $.ajax({
        url: 'proc_php/get_salary.php',
        dataType: 'json',
        success: function(s){
            oTable_salary.fnClearTable();
            for(var i = 0; i < s.length; i++) {
             oTable_salary.fnAddData([
                        s[i][0],
                        s[i][1],
                        s[i][2],
                        s[i][3]                                     
                               ]);                                      
            } // End For                                            
        },
        error: function(e){
           alert(e.responseText);   
        }
        });
}); 

我想在选中时从数据表中获取 ID。我想到了这个,但它不起作用:

$('#jsontable_salary tbody tr').on('click', function (e) {
    e.preventDefault();
    var rowIndex =  $(this).closest('td')[0].text;
    alert(rowIndex);
});

尝试在此上下文中使用 .find(),因为我们正在尝试从其父 tr

的单击事件中检索 td
$('#jsontable_salary').on('click', 'tr', function (e) {
    e.preventDefault();
    var rowIndex =  $(this).find('td:eq(0)').text();
    alert(rowIndex);
});

在 运行 时间内加载的元素必须使用 event delegation 才能附加事件。

你可以试试

var rowIndex =  $(this).find('td').first().text()

您尝试使用 closest 无效,因为 closest 向上遍历 DOM