2014-09-21 21 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]); 
}