我使用jQuery DataTables来显示信息,但当我单击Delete按钮时,最后一个按钮不起作用,所以我是一个新的开发人员。这是我的代码:
var dataTable;
$(document).ready(function () {
loadDataTable();
});
function loadDataTable(){
dataTable = $('#DT_load').DataTable({
"ajax": {
"url": "/api/book",
"type": "GET",
"datatype": "json"
},
"columns": [
{ "data": "name", "width": "25%" },
{ "data": "author", "width": "25%" },
{ "data": "isbn", "width": "25%" },
{
"data": "id",
"render": function (data) {
return `<div class="text-center">
<a href="/BookList/Edit?id=${data}" class="btn btn-success text-white" style='cursor:pointer; width:70px;'>
Edit
</a>
<button class="btn btn-danger text-white" style='cursor:pointer; width:70px;'
onclick=Delete('/api/book?id='+${data})>
Delete
</button>
</div>`;
}, "width":"40%"
}
],
"language": {
"url": "js/Arabic.json"
},
"width":"100%"
})
}
谢谢。
为按钮设置type="button"
。然后将id传递给Delete函数。
<button type="button" class="btn btn-danger text-white" style='cursor:pointer; width:70px;' onclick="Delete(data)">
最后使用ajax调用Delete操作:
function Delete(id) {
$.ajax({
type: "POST",
url: '@Url.Action("Delete")', //delete action
data: JSON.stringify({ id: id }), //use id here
contentType: 'application/json; charset=utf-8',
success: function (data) {
},
error: function (error) {
}
});
}