jQuery DataTable删除具有ID的Tr

时间:2018-08-17 21:03:18

标签: javascript jquery datatables

我的桌子

<table id="grid">
    <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Position</th>
            <th>Action</th>
        </tr>
    </thead>
    <tbody>
        <tr id="row1">
            <td>Tiger Nixon</td>
            <td>System Architect</td>
            <td><button onclick="doDeleteClick('row1')">Delete</button></td>
        </tr>
        <tr id="row2">
            <td>Garrett Winters</td>
            <td>Accountant</td>
            <td><button onclick="doDeleteClick('row2')">Delete</button></td>
        </tr>
        <tr id="row3">
            <td>Ashton Cox</td>
            <td>Junior Technical Author</td>
            <td><button onclick="doDeleteClick('row3')">Delete</button></td>
        </tr>
    </tbody>
</table>

在功能上,我尝试过

$('#grid').DataTable().row($('#row-2')).remove().draw();

使用函数通过发送参数来执行,如何连续执行此操作?

1 个答案:

答案 0 :(得分:0)

只需创建这样的按钮

<button data-id="some-unique-id" class="removeBtn">Delete</button>

然后使用简单的jQuery

$(".removeBtn").on('click', function(){
    $(this).closest('tr').remove();
    var id = $(this).attr('data-id');
    //Do something with the id
});