是否可以根据数据属性删除表行

时间:2016-11-02 07:30:11

标签: jquery

单击删除按钮是否可以根据数据属性删除表行?

这是我的js代码

$(document).on("click", ".deletevideo", function(event) {
    if (confirm("Are You Sure to Remove This Video From This PAC?") == true) {
        var video_id = $(this).data('videoid');
    }

    event.stopImmediatePropagation();
    event.preventDefault();
    return false;
});

Fiddle

2 个答案:

答案 0 :(得分:2)

您可以使用closest()遍历需要删除的tr元素。

if (confirm("Are You Sure to Remove This Video From This PAC?")) {
    //Travese up to get the row and delete
    $(this).closest('tr').remove();
}

Fiddle

答案 1 :(得分:1)

您需要从

传递您想要数据属性的tr的索引
$('#tableid tr:eq(0)'); // First row in table

$('#tableid tr:eq(1)'); // Second row in table

因为表格中可能有多行

var theRowId = $('#tableid tr:eq(1)').attr('data-id'); // Get the Second Row id
$('#tableid tr#'+theRowId).remove();  // Remove the row with id

如果您知道行的ID,则只需执行此操作

$('#tableid tr[data-id="'+theRowId+'"]').remove();