gpt4 book ai didi

javascript - 使用动态创建的按钮单击访问 jQuery 中的数据表内容

转载 作者:行者123 更新时间:2023-11-30 00:20:00 25 4
gpt4 key购买 nike

我是 jQuery 新手。当我按下删除按钮时,我需要访问数据表内容。我尝试了很多方法,但它返回未定义。

$(document).ready(function() {
loadData()
});

function loadData() {

var table = $('#example').DataTable({

"ajax" : {
"method" : 'POST',
"crossDomain" : true,
"dataType" : 'json',
"contentType" : 'application/x-www-form-urlencoded; charset=UTF-8',
"dataSrc" : "",
"url" : "http://localhost:8081/Lakshmi_Service/admin/full"
},

"columns" : [ {
"data" : "adminName"
}, {
"data" : "address"
}, {
"data" : "emailId"
}, {
"data" : "otp"
}, {
"data" : "expiryDate"
}, {
"data" : "mobileNo"
}, {
"targets" : -1,
"data" : null,
"defaultContent" : '<button>Erase</button>'
} ],

"iDisplayLength" : 5,
"bAutoWidth" : true,
"bSort" : false,
"aLengthMenu" : [ [ 10, 25, 50, -1 ], [ 10, 25, 50, "All" ] ],
"bDestroy" : true,
"bFilter" : false,
"bLengthChange" : false
});

$('#example tbody').on('click', 'button', function(event) {
var aData = table.fnGetPosition(this);
var oTableData = table.fnGetData(aData[0]);
var ids = oTableData[aData].adminName;
alert(ids);
});
}

当我点击删除按钮调用另一个服务时,我需要这个 adminName 值。我的页面看起来像:

enter image description here

我的 html 页面是:

<!DOCTYPE html>
<html>
<head>
<script src="jquery-1.11.3.js"></script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script type="text/javascript" language="javascript"
src="https://cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" language="javascript"
src="https://cdn.datatables.net/1.10.9/js/dataTables.bootstrap.min.js"></script>
<script src="jquery-ui.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css"
href="https://cdn.datatables.net/1.10.9/css/jquery.dataTables.min.css">
<link rel="stylesheet" type="text/css"
href="https://cdn.datatables.net/1.10.9/css/dataTables.bootstrap.min.css">

<title>Guru Statistics</title>
</head>
<body>
<table id="example" class="table table-striped table-bordered"
cellspacing="0" width="60%">
<thead>
<tr title="Name">
<th>Name</th>
<th>address</th>
<th>mailId</th>
<th>otp</th>
<th>Date</th>
<th>Mobile No</th>
<th>Erase</th>
</tr>
</thead>
</table>

<script type="text/javascript" src="home.js"></script>
</body>
</html>

最佳答案

您可以将类添加到“列”(我使用 aoColumns)dataTables 对象中的单元格(adminName、地址...),然后在您的click 中使用这段 jQuery > 事件

$(this).parents("tr").find(".adminName").text();

这样你应该能够得到你点击的行的adminName的值

如果你不想添加类(你应该!)我猜你仍然可以通过这种方式获取你的数据:

$(this).parents("tr").find("td").first().text();

但我不鼓励你这样做,就好像有一天你决定在 adminName 之前添加一个新列(例如“Id”)你的代码将检索 Id 而不是adminName,这可能会破坏您的代码。

关于javascript - 使用动态创建的按钮单击访问 jQuery 中的数据表内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33496143/

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