简体   繁体   English

添加新字段时,如何在现有输入字段中保存值?

[英]How to save the values in existing input fields when adding a new one?

This is what my program's body looks like: 这是我程序的主体外观:

<form id = "input">
<input id = "0" >
</form>

<p onclick = "add()"> Add Another</p>

And on clicking the above 然后点击上面

The following function is executed: 执行以下功能:

 var inputArea = document.getElementById("input");
        next = 1;
    function add(){

        inputArea.innerHTML+= "  <input id = " + next+ ">" ;

Where next is the id of new input field. 接下来的是新输入字段的ID。 In this case, since 0 already exists so value of next is 1. One problem that I am encountering with this is that after adding a new input field, the values in all existing input fields are lost. 在这种情况下,由于已经存在0,所以next值是1。我遇到的一个问题是,在添加新的输入字段之后,所有现有输入字段中的值都会丢失。 How to save these values? 如何保存这些值? My attempt is to place this code in function add(): 我的尝试是将此代码放在函数add()中:

    for (i=0;i<next;i++)
    {inputs[i] = document.getElementById(i);
    inputV[i]= inputs[i].value;
inputs[i].value = inputV[i];}

But this does not works.. 但这是行不通的。

  var inputArea = document.getElementById("input"); next = 1; function add(){ inputArea.innerHTML+= " <input id = " + next+ ">" ; var inputs = new Array(); var inputV = new Array(); for (i=0;i<next;i++) {inputs[i] = document.getElementById(i); inputV[i]= inputs[i].value; inputs[i].value = inputV[i];} next++; } 
 <form id = "input"> <input id = "0" > </form> <p onclick = "add()"> Add Another</p> 

You may want to dynamically add elements to your DOM tree like so 您可能希望像这样将元素动态添加到DOM树中

function add() {
    var form = document.getElementById("input");
    var input = document.createElement("input");
    form.appendChild(input);
}

The problem with what you're doing is that when you write inside an input field, the changes are not represented in the HTML code, only in the memory of the browser. 您正在做的问题是,当您在input字段中编写内容时,所做的更改不会在HTML代码中显示,而只会在浏览器的内存中显示。 Thus if you add text through to code to form.innerHTML , the browser is going to reinterpret the text inside the form which will be 因此,如果您将文本添加到form.innerHTML代码中,浏览器将重新解释表单中的文本,

<input id="0"> <input id="1"> ...

and this will result in two empty input of type text being displayed. 并且这将导致显示两个空的type文本input

Edit: you can then add your id tag via 编辑:您可以通过添加您的id标签

function add() {
    var form = document.getElementById("input");
    var input = document.createElement("input");
    input.id = someValue;
    form.appendChild(input);
}

NB please indent your code in a somewhat logical manner. 注意:请以某种合理的方式缩进代码。

The reason this is happening is that the dom, or more specifically inputArea 's innerHtml doesnt get changed when you type into a form field. 发生这种情况的原因是,当您在表单字段中键入内容时,dom(或更确切地说, inputArea的innerHtml)不会被更改。 And what youre doing is resetting the innerHTML with a blank input BEFORE youre capturing the values. 您所做的就是在捕获值之前用空白输入重置innerHTML。

so whats going on is you have HTML like this: 所以这是怎么回事,你有这样的HTML:

<input id='0' />

then type into the form so that it behaves like: 然后输入表格,使其表现为:

<input id='0' value='foo' />

but thats not what the innerHTML actual is. 但这不是innerHTML实际的内容。 its still <input id='0' /> because the value is kept in memory not on the dom. 它仍然<input id='0' />因为该值保存在内存中而不是dom上。

if you want to add new elements to the form, you need to use appendChild instead 如果要向表单添加新元素,则需要使用appendChild代替

so convert 所以转换

inputArea.innerHTML+= "  <input id = " + next+ ">" 

to

inputArea.appendChild(document.createElement('input'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM