Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在动态添加其他文本框时无法保留旧文本框的数据_Javascript_Html_Dom_Asp Classic - Fatal编程技术网

Javascript 在动态添加其他文本框时无法保留旧文本框的数据

Javascript 在动态添加其他文本框时无法保留旧文本框的数据,javascript,html,dom,asp-classic,Javascript,Html,Dom,Asp Classic,大家好,我尝试通过JavaScript在经典ASP中动态添加文本框和其他控件,但遇到了一些问题 虽然我可以添加新的文本框,但无法保留它们的数据 例如:如果我有一个按钮,在单击时我想在html页面中添加文本框,在第一次单击时添加文本框,并在该文本框中输入数据,如STACKOVERFLOW,但当我再次单击按钮时,新的文本框2也被添加,但文本框1数据消失 我把ASP代码改成了简单的HTML,这样就容易了。这是代码 <!DOCTYPE html> <html> <bo

大家好,我尝试通过JavaScript在经典ASP中动态添加文本框和其他控件,但遇到了一些问题

虽然我可以添加新的文本框,但无法保留它们的数据

例如:如果我有一个按钮,在单击时我想在html页面中添加文本框,在第一次单击时添加文本框,并在该文本框中输入数据,如STACKOVERFLOW,但当我再次单击按钮时,新的文本框2也被添加,但文本框1数据消失

我把ASP代码改成了简单的HTML,这样就容易了。这是代码

 <!DOCTYPE html>
 <html>
 <body>

 <div id ='div1' style="margin-bottom:6px;"></div>
 <button onclick="myFunction()">Change link</button>

 <script>
 function myFunction() {
     var p_strContents = '<input name="cust_name_0" id="cust_name_0" value="" type="text" size="0" class="FormTextField" maxlength="6" title="">';
     document.getElementById('div1').innerHTML = document.getElementById('div1').innerHTML + p_strContents ;
     //document.getElementById('div1').appendChild(p_strContents);
 }
 </script>

 </body>
 </html>

更改链接
函数myFunction(){
var pu strContents='';
document.getElementById('div1')。innerHTML=document.getElementById('div1')。innerHTML+p_strContent;
//document.getElementById('div1').appendChild(p_strContent);
}
我甚至尝试使用appendchild,但它会抛出一些错误

如果你想直接在线试用,请使用此链接并粘贴我的代码,这样你们就很容易检查了。

我正在使用JavaScript


你们能帮帮我吗。关于…

请检查以下内容:

function myFunction() {     
    var textBx = document.createElement('input');
    textBx.setAttribute('type', 'textbox');
    textBx.setAttribute('class', 'FormTextField');
    textBx.setAttribute('maxlength', '6');
    document.getElementById('div1').appendChild(textBx);
    textBx = null;
 }

不要设置重新创建所有内容的innerHTML,而是使用或。如果您尝试了appendChild,则必须将元素节点传递给它,而不是htmli使用的appendChild,但它会抛出一些错误,表明parameter1不是节点。如何将其作为节点传递???。非常感谢,它像一个符咒一样工作。为什么最终要将textBx设置为null???这在这里并不重要,但如果在一个范围内创建并附加多个元素,则会产生性能问题。