【问题标题】:Cannot retain the data of old Text-boxes on dynamic addition of others不能保留旧文本框的数据动态添加其他
【发布时间】:2015-05-24 03:31:38
【问题描述】:

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

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

例如:如果我有一个按钮,并且单击时我想在第一次单击时将文本框添加到 html 页面,则会添加一个文本框,然后我在该文本框中输入数据,例如 STACKOVERFLOW,但是当我再次点击按钮时,也添加了新的 textbox2 但textbox1 数据消失。

我将我的 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>

我什至尝试使用 appendchild,但它会引发一些错误。

如果您想直接在线尝试,请使用此链接并粘贴我的代码,以便你们检查。 http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_elmnt_innerhtml

我正在使用 JavaScript。

你们能帮帮我吗?问候...

【问题讨论】:

  • 不要设置重新创建所有内容的innerHTML,请改用appendChild 或insertAdjacentHTML。如果您尝试过 appendChild,则必须将其传递给元素节点,而不是 html
  • 我使用了 appendchild,但它引发了一些错误,表明 parameter1 不是节点。我怎样才能将它作为节点传递??。

标签: javascript html dom asp-classic


【解决方案1】:

请检查:

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;
 }

Please check working code here

【讨论】:

  • 非常感谢它就像一个魅力。为什么你最终将 textBx 设置为 null ???
  • 这里没关系,但是如果你在一个范围内创建和附加多个元素会产生性能问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 2013-11-19
  • 1970-01-01
  • 2012-12-16
  • 2022-01-11
  • 1970-01-01
相关资源
最近更新 更多