从我从循环(JQuery)中收集的表中删除行

Remove rows from a table which I collected from a loop (JQuery)

本文关键字:删除行 循环 JQuery      更新时间:2023-09-26

我有一个表,我用JQuery循环以查找符合某些条件的行:

 $('#sometable').find('tr').each(function () {  
   var row = $(this); //<----
    if(row.find('input[type="checkbox"]').is(':checked')) {   
//etc
   }   
}

我的问题是,有没有办法删除每个匹配的行?我的意思是,有没有一种方法可以在if(row.find('input[type="checkbox"]').is(':checked'))中收集这些行变量,这样我就可以直接从表中删除特定于
请注意,我的行没有唯一的id

您可能需要:

$('#proposedtable tr:contains(input:checkbox:checked)').remove();

$('#proposedtable input:checkbox:checked').closest('tr').remove();

试试这个:

var filteredRows = $('#sometable').find('tr').filter(function(){
    return $(this).find('input[type="checkbox"]').is(':checked'));
});
$(filteredRows).remove();

上面的函数将收集所有行(tr),然后根据复选框的选中状态过滤这些行。稍后将删除筛选的行。


要使其成为数组,请使用array.prototype.stice.call()

var arrFilteredRows = Array.prototype.slice.call(filteredRows);

这对你有帮助吗?

http://jsfiddle.net/LGdA3/

<pre><code>
    $('input#myButton').on('click', function(){
        $('table#someTable td input[type="checkbox"]:checked').each(function(){
           $(this).parents('tr').first().remove(); 
        });
    });
</code></pre>