2014-08-31 80 views
1

我試圖在點擊一個按鈕時刪除一行。它工作正常,除非我在一個表內。我的JSP在表中有50行的組合,之後我們嵌套這些表。現在,我的問題是如果我想刪除上一個表中的最後一行,我應該怎麼做?使用JavaScript刪除節點

我附上UI和調試器HTML視圖的屏幕截圖。任何建議都會有所幫助。

[DROW]這裏是我們有一個樹結構的不同行。我們需要首先爬到每一行,然後向下移動到lastChild以刪除最後一行。

這是唯一可能的,如果我的previousRow包含最後一行val。我無法理解它。

function removeAnotherResponse(arg0, data, response, request){ 

// Make sure the response has everything we need 
if(arguments==null || arguments.length<4 || !request.content || request.content==null || !request.content.groupUniqueId || request.content.groupUniqueId==null){ 
    return; 
} 

// Check for true response 
if(!data || data==null || !data[0] || !data[0].removedSuccess || data[0].removedSuccess!=true){ 
    return; 
} 

var groupUniqueId=request.content.groupUniqueId; 

// Get the add another button row and add the new row before it. 
var addAnotherRow=document.getElementById("ADD_"+groupUniqueId); 
if(addAnotherRow==null) return; 

    var previousRow=document.getElementById("ADD_"+groupUniqueId).previousSibling; 
    addAnotherRow.parentNode.removeChild(previousRow); 



showOrHideAddRemoveButtons(groupUniqueId, data[0].removeButton, data[0].addButton); 
updateSectionCount(groupUniqueId, false); 
getPortCount(groupUniqueId); 

}

+0

添加代碼不是截圖。 – Mritunjay 2014-08-31 13:02:10

+0

@Mritunjay ..我添加了我用來刪除表內行的代碼。我無法移動到上面嵌套了表格的行,如調試屏幕截圖所示。 – Bhaskar 2014-08-31 13:05:41

+1

'id'屬性值應該是每個元素唯一的。在這裏你應該使用不具有相同值的'id'類。 – GillesC 2014-08-31 13:14:16

回答

0

謝謝大家提示我改正html代碼。

UI元素確實爲我在其他方面造成了問題。現在,我不是創建一個新的,而是找到了處理這個問題的其他方法,而且我的HTML代碼變得更簡單了。 PFB腳本已經改變了。感謝所有的建議:)

if (document.getElementById("ID" + gID) != null) { 

    innerElement = document.getElementById("ID_TD_" + gID); 
    innerElement.innerHTML = innerElement.innerHTML + responseHTML; 

} else { 

    innerElement = document.createElement("td"); 
    innerElement.setAttribute("id", "ID_TD_" + gID); 

    newElement.appendChild(innerElement); 
    innerElement.innerHTML = responseHTML; 

} 
2

您發佈的HTML片段似乎有一些問題(ID重複,使用<tr>作爲其他<tr>父)。我仍然回答你的主要問題如何刪除表格的行。您可以使用DOM table對象的deleteRow方法。 rows集合可用於訪問現有行,deleteRowinsertRow允許您修改表。

+0

在這裏,我想移動到那一行,就像我目前的做法一樣。當我在表格中遍歷時,可以使用上面的代碼輕鬆完成此操作。但是當一個表中的所有50行都被刪除,並且我想要移動到上面的行時,它又顯示了一個表,我無法這樣做。 – Bhaskar 2014-08-31 13:33:05

+1

@Bhaskar:對不起,但我不能完全關注你。您發佈的代碼片段僅使用像'parentNode'和'removeChild'這樣的DOM方法,它們對於每個HTML元素都是** common **。 DOM API有一些**特定的**方法,比如'deleteRow',它允許您在使用**特定的** DOM元素(如「

」)時簡化工作。你最主要的問題是:*如何刪除某個特定表的最後一行?*。我建議你按ID選擇表格,然後用'deleteRow'來刪除你想要刪除的行。 – Oleg2014-08-31 13:42:05

+0

感謝您提出改變建議。可能我會先試着修復我的jsp,如果我的問題得到解決,我會發布我的答案。 – Bhaskar 2014-09-01 12:47:01