我是jquery的新手。当我按下擦除按钮时,我需要访问数据表内容。我尝试了很多方法,但它返回undefined。提前谢谢。
$(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值。我的页面看起来像..
我的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>
&#13;
答案 0 :(得分:1)
您可以做的是在&#34;列&#34;中为您的单元格添加类(adminName,address ...)。 (我使用aoColumns)dataTables对象,然后在click
事件中使用这段jQuery
$(this).parents("tr").find(".adminName").text();
这样你就可以获得你在
中点击的行的adminName的值如果你不想添加课程(你应该!),你仍然可以通过这种方式获取数据:
$(this).parents("tr").find("td").first().text();
但我不鼓励你这样做,好像有一天你决定在adminName之前添加一个新列(例如&#34; Id&#34;例如),你的代码将检索{{ 1}}而不是Id
,这可能会破坏你的代码。