2011-06-20 53 views
0

我有一個表,其中包含假設4列,我想刪除從第一行到最後一行包含0的列。在這種情況下,我想刪除第二&第4列我如何才能做到這一點使用jQuery我如何刪除包含0的列使用jquery

1 0 10 0 
2 0 20 0 
3 0 30 0 

O/P應

1 10 
2 20 
3 30 
+1

作爲HTML表?或者是什麼? –

+0

*整列*包含「0」還是可能發生,只是某些單元格包含「0」?因爲如果只刪除一些單元格並留下其他單元格,它將會破壞表格。 – Tomalak

+0

你爲什麼需要這個?我認爲即使數據爲零,這些列也包含有用的數據。如果這些列不重要,那麼爲什麼你要綁定它? –

回答

0
$("table td") 
.filter(function() { 
    // cellIndex will be 0 for the first column, 1 for the second, and so on 
    return (this.cellIndex == 1 || this.cellIndex == 3); 
}) 
.remove(); 
1

要清除其中包括所有列只有零,您可以首先確定第一行中包含0的單元格,對於這些單元格,檢查該列中包含多少個包含單元格的單元格。如果它匹配總行數,然後刪除它們。

var rows = $('tr').length; 
$('tr:first td:contains(0)').each(function(){ 
    var i = $(this).index()+1; 
    var s = $('tr td:nth-child('+i+')'); 
     if(s.filter(':contains(0)').length==rows) s.remove(); 


}); 

例如:http://jsfiddle.net/niklasvh/frdsc/