2014-09-21 20 views
0

我想從我的表單中刪除一個obj,並且能夠在它加載時將其添加回來。但是,當我嘗試添加它回來使用var我試圖保存onload我得到一個錯誤(它查詢當前窗體有其子刪除)。如何在當前窗體onload中存儲obj屬性?

如何將所有子對象的屬性存儲在當前窗體的onload中,以便我可以在從文檔中刪除後將它們添加回來?

這會給我一個錯誤( 「論證Node.appendChild 1是不是對象」):

var fullList2 = null; 

window.onload = function() { 
    fullList2 = document.getElementsByName('person')[0].children; 
    document.getElementsByName('person')[0].removeChild(document.getElementsByName('person')[0].children[1]); 
    document.getElementsByName('person')[0].appendChild(fullList2[1]); 
} 

形式:

<form name="trans" id="trans" method=POST action=entertransaction.jsp> 
    <input type="text" onkeyup="update();" id="input1" value="" name="filterTxt" size="21" tabindex="1" style="width: 195px" /> 
    <br /> 
    <select id="person" name="person" size=10 tabindex="2" style="width: 200px"> 
     <option value="0">Scott Ambler</option> 
     <option value="1">Andrew Binstock</option> 
    </select> 
    <br /> 
    <input type=submit name=action value="Next ->" tabindex="3" /> 
</form> 
+0

爲什麼要刪除並追加?只是爲了改變它的立場? – Bergi 2014-09-21 11:16:44

回答

0

你fullList2只是給一個參考person select元素的子數組,當你從person中移除child時,選擇你也將它們從fullList2數組中移除,這就是爲什麼你的方法不起作用。

至於解決方案:你將不得不自己存儲子元素。所以,你的代碼看起來是這樣的(未測試只是從我的頭頂書面):

var fullList2 = []; 

window.onload = function() { 
    var person= document.getElementsByName('person')[0]; 
    //Remove objects from the dom and store them in fullList2 
    fullList2.push(person.removeChild(person.childNodes[0]); 
    fullList2.push(person.removeChild(person.childNodes[1]); 
    //Add them back in 
    person.appendChild(fullList2[0]); 
    person.appendChild(fullList2[1]); 
}