使用 table 名称删除 jquery 中的 table 行

Remove table row in jquery using table name

我正在遍历选中的 table 行,如果某个参数为真,我想删除当前行,但到目前为止,我删除了错误的行。任何帮助表示赞赏。这就是我的。

$('input[type=checkbox]').each(function() { 
    if (this.checked == true) {
        var del_id = $(this).closest('tr').attr('data-id');

        $.each(del_arr, function(index, value ) {
            if (value == del_id) {
                $('#spreadsheetstable tr:last').remove();
                console.log('Removing :'+  del_id +' And ' + value);
            }
        });
    }
});

像这样:

    $('input[type=checkbox]:checked').each(function() {

    var $tr =  $(this).closest('tr');

    var del_id = $(this).closest('tr').attr('data-id');

    $.each(del_arr, function(index, value ) {

        if (value == del_id){
            $tr.remove();
            console.log('Removing :'+  del_id +' And ' + value);
            return false;
        }
    });
})

如果您正在使用 jQuery DataTables,最好使用 DataTables API 方法 row().remove() 删除行,而不是直接操作 DOM。

$('input[type=checkbox]:checked').each(function() {
   var $tr =  $(this).closest('tr');
   var del_id = $tr.attr('data-id');
   $.each(del_arr, function(index, value ) {
      if (value == del_id){
         $('#spreadsheetstable').DataTable().row($tr).remove().draw(false);

         console.log('Removing :'+  del_id +' And ' + value);
         return false;
      }
   });
});