点击一行,我想获取它的数据。确切地说,我实际上总是需要输出“id”。
<table id="projects">
<thead>
<tr>
<th>Title</th>
<th>Company</th>
<th>Text</th>
<th>ID</th>
</tr>
</thead>
</table>
var projects = $('#projects').DataTable({
"ajax": {
"url": "data/projects.json",
"dataSrc": "",
},
"columns": [
{
"data": "title"
},
{
"data": "company"
},
{
"data": "text"
},
{
"data": "id"
}
],
});
$('#projects tbody').on('click', 'tr', function () {
var data = projects.row( this ).data();
alert( 'You clicked on '+data[0]+'\'s row' );
} );
projects.json:
[{
"id": "24",
"title": "Animals",
"text": "something",
"company": "inc"
},
这给了我输出:
You clicked on undefined's row
我需要的是:
$('#projects tbody').on('click', 'tr', function () {
var data = projects.row( this ).data();
alert( 'You clicked on '+ row["id"]+'\'s row' );
} );
但这并没有给我提醒。
答案 0 :(得分:1)
您可以从一行访问数据:
$(this).find('td:eq(3)');
证明: https://jsfiddle.net/tw0a024t/
row[]
无效,因为jquery正在寻找html显示而不是对象。