gpt4 book ai didi

javascript - 如何从Datatable单元格获取内部html值

转载 作者:行者123 更新时间:2023-11-28 08:18:01 25 4
gpt4 key购买 nike

我是数据表的新手。在这里,当我单击与该行关联的 View 链接时,我尝试获取该行的第一个单元格值,而不是我获取的值 [object object]。

这是我的代码

        $(document).ready(function() {


// Delete a record
$('#example').on('click', 'a.editor_view', function (e) {
e.preventDefault();
var rowIndex = oTable.fnGetPosition( $(this).closest('tr')[0] );
aData = oTable.fnGetData($(this).parents('tr')[0]);
alert(aData);
} );

// DataTables init

var oTable=$('#example').dataTable( {
"sDom": "Tfrtip",
"sAjaxSource": "php/browsers.php",
"aoColumns": [
{ "mData": "browser" },
{ "mData": "engine" },
{ "mData": "platform" },
{ "mData": "grade", "sClass": "center" },
{
"mData": null,
"sClass": "center",
"sDefaultContent": '<a href="" class="editor_view">view</a> / <a href="" class="editor_remove">Delete</a>'
}
]
} );
} );

HTML 表格:

<table cellpadding="0" cellspacing="0" border="0" class="display" id="example" width="100%">
<thead>
<tr>
<th width="30%">Browser</th>
<th width="20%">Rendering engine</th>
<th width="20%">Platform(s)</th>
<th width="14%">CSS grade</th>
<th width="16%">Admin</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Browser</th>
<th>Rendering engine</th>
<th>Platform(s)</th>
<th>CSS grade</th>
<th>Admin</th>
</tr>
</tfoot>

现在,当我单击 View 时,我需要导航到 id 类似的另一个页面view.php?id=125

谢谢

最佳答案

$('#example').on('click', 'a.editor_view', function (e) {
e.preventDefault();
var rowIndex = oTable.fnGetPosition( $(this).closest('tr')[0] );
aData = oTable.fnGetData(rowIndex,0);
alert(aData);
} );

来自 API 文档:

fnGetData输入参数:
{int|node}:TR行节点、TD/TH单元节点或整数。如果作为 TR 节点给出,则将返回整行的数据源。如果作为 TD/TH 单元节点给出,则将自动计算 iCol 并返回单元的数据。如果以整数形式给出,则将其视为该行的 aoData 内部数据索引(请参阅 fnGetPosition)以及所使用的该行的数据。

{int}:您想要其数据的可选列索引。

关于javascript - 如何从Datatable单元格获取内部html值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23372838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com