简体   繁体   English

Javascript添加的表单元素未添加到数组

[英]Javascript added form elements not being added to array

I have a simple Javascript that adds text boxes to a form. 我有一个简单的Javascript,可将文本框添加到表单中。 It appears to work (ie a new box is added to the form) but when I submit the new form elements are not added to the array. 它似乎可以工作(即在表单中添加了一个新框),但是当我提交新表单时,元素未添加到数组中。 So only the info in the first text box is submitted. 因此,仅提交第一个文本框中的信息。 Here is the code. 这是代码。

HTML HTML

<div id="dynamicInput">
      Part SKU 1: <input type="text" name="myInputs[]">
 </div>
 <input type="button" value="Add Another Part" onClick="addInput('dynamicInput');">

JS JS

var counter = 1;
var limit = 5;
function addInput(divName){
 if (counter == limit)  {
      alert("You have reached the limit of adding " + counter + " inputs");
 }
 else {
      var newdiv = document.createElement('div');
      newdiv.innerHTML = "Part " + (counter + 1) + " <br><input id='input"+(counter + 1)+"' type='text' name='myInputs[]'>";
      document.getElementById(divName).appendChild(newdiv);
      counter++;
 }
}

Any help would be appreciated. 任何帮助,将不胜感激。

您的输入名称必须唯一,否则它将抓取第一个,而忽略其余的,类似于JS处理ID的方式

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

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