我的 JavaScript 中有这两行代码,它们创建两个新的 HTML 表单,以便用户在每次调用包含函数时输入数据。现在,每次创建新的 2 个表单时,前一个表单的输入都会被删除,我想我知道为什么,它与第二行以及我如何设置 innerHTML 有关。我似乎无法找出另一种方法来做到这一点,但是,非常感谢任何帮助!
谢谢。
var newForm = "<b>Checkpoint " + markerId + ":</b> Name <input type='text' id=" + markerId + "> Description <input type ='text' id=" + markerId + "desc style='width:480px;height:58px;'> <button type='submit' value='Remove' onClick='remove_checkpoint();' /> <br />";
document.getElementById("divForms").innerHTML = document.getElementById("divForms").innerHTML + "<br />" + newForm;
最佳答案
了解 DOM 方法,如 createElement和 appendChild 。
var div = document.createElement("div");
div.innerHTML = "<b>Your code</b>";
document.getElementById("divForms").appendChild(div);
关于javascript - 创建新的 HTML 表单而不重置先前的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16020536/