【发布时间】:2019-03-31 10:33:27
【问题描述】:
我的 jquery DataTable 中的每一行都有一个按钮。我想在我点击按钮时提醒所选记录的第一列的值。
问题是,我的按钮代码不起作用。
这是我遵循的文档:https://datatables.net/examples/ajax/null_data_source.html
这是我的代码:
jQuery
$(document).ready(function () {
$.ajax({
type: "POST",
url: "WebService1.asmx/Bind",
dataType: 'json',
success: function (data) {
$('#datatable').DataTable({
data: data,
columns: [
{ 'data': 'CenterID' },
{ 'data': 'CenterName' },
{ 'data': 'LicenseKey' },
{ 'data': 'ExpiryDate' },
{ 'data': 'YearID' },
{ 'data': 'Date' },
// This is where I add the button
{ "defaultContent": "<button>Edit</button>"}
]
});
}
});
//This is where I make an attempt to add functionality for the button in the datatable
$('#datatable tbody').on('click', 'button', function () {
var data = table.row($(this).parents('tr')).data();
alert("The value is: " + data[0]);
});
});
HTML
<table id="datatable" class="display" style="width: 100%">
<thead>
<tr>
<th>CenterID</th>
<th>CenterName</th>
<th>LicenseKey</th>
<th>ExpiryDate</th>
<th>YearID</th>
<th>Date</th>
<th>Action</th>
</tr>
</thead>
</table>
请帮忙。谢谢。
【问题讨论】:
-
检查点击按钮时浏览器检查元素是否有任何错误。
-
不,没有错误。我已经检查过了。只是回了一个帖子,它没有到达我的 js 代码。
标签: javascript jquery datatable