2016-08-11 50 views
0

我有一個顯示過濾器列表的兩列的表,並且這些過濾器可以通過點擊一個小「X」圖標旁邊的每個被刪除:刪除錶行特定的細胞已被清空的jQuery

<tr> 
    <td class="filter-point">State is</td> 
    <td class="tag-column"><div class="tag">California <i class="fa fa-close"></i></div></td> 
</tr> 

$("td.tag-column .tag i").click(function(){ 
    $(this).parent().remove(); 
}); 

我希望能夠刪除整個行,如果所有的標籤已被刪除(如果td.tag列是空的),但我似乎無法找出正確的語法或方法來附加行排空到標籤清空。

回答

1

只是延長目前的點擊事件來檢查,每個標籤取出後,如果行有任何標籤左:

$("td.tag-column .tag i").click(function(){ 
    var td = $(this).closest('td'); 
    $(this).parent().remove(); //remove tag 
    if (!td.children('.tag').length) td.parent().remove(); //if no tags left, remove row 
}); 
1

你可以像下面。

$("td.tag-column .tag i").click(function() { 
    $(this).parent().remove(); 
    var tr = $(this).closest('tr'); 
    tr.find('.tag').length || tr.remove(); 
});