我有一個表,其中包含假設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
我有一個表,其中包含假設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
你需要通過所有的細胞來檢查零迭代每列中,這可能會幫助: How to loop through table cells with jQuery and send the data to database
$("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();
要清除其中包括所有列只有零,您可以首先確定第一行中包含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();
});
作爲HTML表?或者是什麼? –
*整列*包含「0」還是可能發生,只是某些單元格包含「0」?因爲如果只刪除一些單元格並留下其他單元格,它將會破壞表格。 – Tomalak
你爲什麼需要這個?我認爲即使數據爲零,這些列也包含有用的數據。如果這些列不重要,那麼爲什麼你要綁定它? –