简体   繁体   English

按钮 onclick 不起作用(删除),使用数据表

[英]button onclick doesn't work (DELETE), Using DATATABLE

I'm using jQuery DataTables to display information, but when i click on the Delete button the last does not work, such i am a new developer.我正在使用 jQuery DataTables 来显示信息,但是当我单击删除按钮时,最后一个不起作用,因此我是一个新的开发人员。 This is my code :这是我的代码:

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>
                        &nbsp;
                        <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%"

    })
}

Thank you .谢谢你 。

Set type="button" for your button.为您的按钮设置type="button" Then pass id to the Delete Function.然后将 id 传递给 Delete 函数。

<button type="button" class="btn btn-danger text-white" style='cursor:pointer; width:70px;' onclick="Delete(data)">

At the end use ajax to call your Delete action :最后使用 ajax 调用您的删除操作:

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) {

      }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM