使用for循环从文本Form中动态填充数组

时间:2015-05-20 19:37:10

标签: javascript arrays

我正在尝试a)创建一个dymic表单,可以添加更多的输入行,然后b)使用这些行中的文本通过模板创建单独的文本输出。我正在使用for循环来动态填充我试图用来存储信息的数组。

以下是代码:

function Start_TemplateBuilder(){

var A = [10];

var B = [10];

var Etc = document.getElementById("Etc"); /*There are multiple instance of this created dynamically via javascript from a form, which labels them as Etc1, Etc2, etc. */

var More = document.getElementById("More");  /*There are multiple instance of this created dynamically via javascript from a form, which labels them as More1, More2, etc.  */

A[0] = document.getElementById("Etc");

B[0] = document.getElementById("More");

var text_compile = '';

for (var comparison = 1; comparison < entry_iterative; comparison++){                 

/* entry_iterative is defined as a global variable outside of this function and starts at 1 */

A[comparison] = document.getElementById(Etc + comparison);                       /* Here’s where I’m trying to use a for loop to populate Etc 1 thru 10 and More 1 thru 10 as needed, and trying to reference specific entries in A based on the comparison variable */

B[comparison] = document.getElementById(More + comparison);
}


/* more below, but this is where I’m hitting a wall */

似乎没有在for循环中正确填充数组。是否可以使用java脚本动态填充数组,还是需要使用if语句的主机?

如果有帮助,这里是用于动态更新表单的代码:

function Add_Entry(){

text_0b +=                          "<br><input type = 'text' id = 'Etc" + entry_iterative + "' value = 'Enter the next entry' />";

text_0b +=                          "<br><input type = 'text' id = 'More" + entry_iterative + "' value = 'Enter protoc of the next entry in (X) format' />";

text_0 = text_0a;  /* Global variables defined outside the function */
text_0 += text_0b;
text_0 += text_0c;
output_0.innerHTML = text_0;

entry_iterative++;
}

0 个答案:

没有答案