2016-08-24 145 views
-1

我想從我的表中刪除所有空行,但每次我點擊按鈕來做到這一點,表頭也被刪除。從表中刪除空行jQuery

我該如何解決這個問題?

這裏的小提琴:

FIDDLE

要重現該問題,你需要做的是:

  1. 點擊Edit Properties
  2. 單擊Add Row並添加一些屬性(請確保將一行完全清空)。
  3. 點擊Save
  4. 點擊Remove Empty Rows
+3

「使用TBODY尋求調試幫助的問題(「爲什麼這個代碼不工作?」)必須包含所需的行爲,特定的問題或錯誤以及在問題中重現**的最短代碼精靈**。 「沒有一個明確的問題陳述的問題是沒有用的其他讀者。」 –

+0

@JasonP它可能是我,但我真的沒有看到我怎麼能讓我的問題更清晰地給你...我已經包括一個JSFiddle所有我寫的代碼,以及如何重現問題的詳細說明,你想要更多什麼? – laker001

+1

stackoverflow規則規定,與問題相關的代碼應該在問題**中發佈**, stackoverflow website。jsfiddle是一個很好的補充,但代碼應該仍然是**文本中的問題**。 –

回答

1

問題是,您正在尋找第3行的td標籤,而您的頭部沒有td標籤。因此文本是空的,它會被刪除。

+0

謝謝,超級簡單修復! – laker001

+0

修復http://jsfiddle.net/v4n2vhd0/ –

1

http://jsfiddle.net/a8wLy18w/

我加了TBODY這樣你就可以從頭部

<table align="center" class="table table-bordered table-hover hide4" id="tab_properties"> 
     <thead> 
     <tr> 
      <th class="text-center col-lg-3"> Property </th> 
      <th class="text-center col-lg-4"> Value </th> 
      <th class="text-center col-lg-1"></th> 
     </tr> 
     </thead> 
     <tbody> 

     </tbody> 
    </table> 

然後,我改變你的附加區分身體,並刪除了jQuery

$('#click').on('click', function() { 
    $("#tab_properties tbody tr").each(function() { 
    var cell = $.trim($(this).find('td').text()); 
    if (cell.length == 0) { 
     console.log('empty'); 
     $(this).addClass('nodisplay'); 
    } 
    }); 
}); 

$('#add_property').unbind('click').click(function() { 
    $('#tab_properties tbody').append("<tr><td><input id=\"pn" + properties_id + "\" class=\"editable-input\"></td><td><input id=\"pv" + properties_id + "\" class=\"editable-input\"></td><td><div id=\"pr" + properties_id + "\" class=\"glyphicon glyphicon-remove remove_property\" type=\"button\" style=\"cursor:pointer\"></div></td></tr>"); 
    properties_id++; 
}); 
+0

謝謝,這是另一種方式來做到這一點! – laker001