【发布时间】: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