简体   繁体   English

根据动态添加的表单字段中的 HTML 输入动态创建变量,然后在 for 循环字符串中使用它们

[英]Dynamically create variables based on HTML inputs made into dynamically added form fields, then use them in a for loop string

I've played around with for loops many times in the past, but this is going way over my head when I want to use it with values from IDs in HTML, and moreover put these, separated, into a string.我过去曾多次使用 for 循环,但是当我想将它与 HTML 中的 ID 值一起使用时,这让我头疼,而且将它们分开放入一个字符串中。

I have a simple program so far with the help of another question that lets me add and remove input fields in HTML.到目前为止,在另一个问题的帮助下,我有一个简单的程序,可以让我在 HTML 中添加和删除输入字段。 However, I want to assign each form field's input value to a variable whenever one is added.但是,每当添加一个变量时,我想将每个表单字段的输入值分配给一个变量。 Playing around, this seems to be difficult, because I can't call upon an ID in HTML in a for loop fashion.到处玩,这似乎很困难,因为我不能以 for 循环方式调用 HTML 中的 ID。

At the end of it all, I wanted to put these into a dynamic string when new fields are added:最后,我想在添加新字段时将它们放入动态字符串中:

"The colors consist of " + colorNum0 + " and " + colorNum1 + ". They are all really cool colors."

But whenever a new variable is added (inputted into an added field), I want it to add another " and " + colorNum#.但是每当添加一个新变量(输入到添加的字段中)时,我希望它添加另一个“和”+ colorNum#。

The program seems to get confused if I add variables ahead of time that do not exist yet, or ever, if I do not add the form field for them.如果我提前添加尚不存在的变量,或者如果我不为它们添加表单字段,程序似乎会感到困惑。

Of course, creating a for loop doesn't work when I try something like:当然,当我尝试以下操作时,创建 for 循环不起作用:


var colorNum[i] = document.getElementById("field" + i).value

Because... that's not allowed.因为……这是不允许的。 I'm having trouble figuring out how to make this happen.我很难弄清楚如何做到这一点。 Right now I called out variables for colorNum, but I'm trying to do it dynamically.现在我为 colorNum 调用了变量,但我正在尝试动态地执行它。 This is difficult when trying to get a value from an ID and transferring it to a for loop.这在尝试从 ID 获取值并将其传输到 for 循环时很困难。

HTML: HTML:

<ul id="fields">
</ul>
<a onclick="createinput()">Create an input</a>

<p id="commentHere"><p>

JS: JS:

var count = 0;
window.createinput = function(){
    var field_area = document.getElementById('fields')
    var li = document.createElement("li");
    var input = document.createElement("input");
    input.id = 'field' +count;
    input.name = 'field'+count;
    input.type = "text";
    li.appendChild(input);
    field_area.appendChild(li);
    //create the removal link
    var removalLink = document.createElement('a');
    removalLink.className = "remove";
    removalLink.onclick = function(){
        field_area.removeChild(li); 
        count--;
    }
    removalLink.appendChild(document.createTextNode('Remove Field'));
    li.appendChild(removalLink);
    count++;
    console.log(input.id);
  console.log("Count: " + count);
  
var colorNum0 = document.getElementById("field0").value;
var colorNum1;
var colorNum2;
  console.log(colorNum0);
  
var comment = "The colors consist of " + colorNum0 + " and " + colorNum1 + ". They are all really cool colors.";
  
document.getElementById("commentHere").innerHTML = comment;
  
}

You can get values of all inputs, using the following code snippet:您可以使用以下代码片段获取所有输入的值:

function getInputsData() {
  const inputs = document.getElementsByTagName('input');
  const data = [];
  for (let inp of inputs) {
    data.push(inp.value);
  }
  return data;
}

Considering that you don't have any other input fields.考虑到您没有任何其他输入字段。 If you have, you can assign each input a class or id, as you did, and get the values:如果你有,你可以像你一样为每个输入分配一个类或 id,并获取值:

function getInputsData() {
    const data = [];
    const inputs = document.getElementsByClassName('field');
    for (let inp of inputs) {        
        data.push(inp.value);
    }
    return data;
}

For this case, I've added class for each input field:对于这种情况,我为每个输入字段添加了类:

input.classList.add('field')

Bergi's comment helped me enough for what I had. Bergi 的评论对我所拥有的帮助已经足够了。 I will not be checking this today so I thought I should mark it answered for now.我今天不会检查这个,所以我想我现在应该把它标记为已回答。 What I had was fine, I just had to assign colorNum to an array.我所拥有的很好,我只需要将 colorNum 分配给一个数组。 As for putting this in my string, I'll have to play around a bit another day - doesn't need to be in a loaded question.至于把它放在我的字符串中,我将不得不改天再玩一下 - 不需要在一个加载的问题中。

Edit: Unless someone has an idea for the string?编辑:除非有人对字符串有想法? I am going to try and figure out how to list each item in the array in the string separated by text without creating dozens of new variables for each input.我将尝试弄清楚如何在以文本分隔的字符串中列出数组中的每个项目,而无需为每个输入创建数十个新变量。

Edit 2: I figured out how I will do the part above.编辑2:我想出了我将如何做上面的部分。 After I do have the array, I just have to convert to a string and then split/insert/other string work from there.在我拥有数组之后,我只需转换为字符串,然后从那里拆分/插入/其他字符串即可。

var colorNumX = [];
for (i = 0; i <= count - 1; i++) {
   colorNumX[i] = document.getElementById("field" + i).value;
}

var colorString = colorNum.toString();

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

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