如何使用jQuery删除多个表行

时间:2018-12-19 13:26:15

标签: jquery ajax

我有一个具有唯一行ID的动态表,例如,,每行都有一个复选框。

使用jQuery,我获取了该复选框的所有选定ID(以逗号分隔,例如1,25,4)。我只需要在jquery成功后删除那些选定的tr。  看到我下面的代码:

获取逗号分隔的ID:

var ids = $(".chk:checked").map(function() {
                    return this.id;
                }).get().join(",");

条件:

if(response == 0){
                            alert('Sorry! There is some problem in server, try again.');
                            return false;
                        }else {
                            alert("Successfully removed from library.");
                            $('#tr_'+ids).remove();
                        }

enter image description here

2 个答案:

答案 0 :(得分:0)

$('#tr_'+ids)最终看起来像$('#tr_1,3,7,9'),它是无效的选择器

您可以执行以下操作:

$("tr").has('.chk:checked').remove()

// OR 

$(".chk:checked").closest('tr').remove()

答案 1 :(得分:0)

如果将所有ID添加到数组,则可以使用它删除行

$.each(ids, function(key, val){
    $('#tr_' + val).remove();
});
相关问题