简体   繁体   English

使用 javascript 中的 json 的值填充循环生成的表单字段

[英]populate loop generated form fields with values from json with javascript

I have a webpage using javascript and jquery.我有一个使用 javascript 和 jquery 的网页。

I am trying to dynamically create content via a for loop.我正在尝试通过 for 循环动态创建内容。

The loop is over a array of json objects.循环在 json 对象数组上。 Each object has a week string and a number integer.每个 object 都有一个星期字符串和一个数字 integer。

each loop creates two buttons and a form field.每个循环创建两个按钮和一个表单域。 The number needs to populate the form field.该数字需要填充表单字段。

When the loop executes the buttons and the form fields get created however the number only populates the first form field.当循环执行按钮并创建表单字段时,数字仅填充第一个表单字段。

It populates it with every number.它用每个数字填充它。

so given the array所以给定数组

var testobject = [
      {
        week: '9/15/20',
        number: 6
      },
      {
        week: '10/13/20',
        number: 70
      },
      {
        week: '11/17,20',
        number: 34
      }
    ];

the first form field will populate with the number 6 then replaced by 70 then replaced by 34.第一个表单字段将填充数字 6,然后替换为 70,然后替换为 34。

Rather each form field should have the number that corresponds to its iteration in the for loop.相反,每个表单字段都应该具有与其在 for 循环中的迭代相对应的数字。

first form field should have 6 second form field should have 70 third form field should have 34第一个表单域应该有 6 第二个表单域应该有 70 第三个表单域应该有 34

here is the code in my loop:这是我循环中的代码:

for( i  = 0; i < testobject.length; i++){
        console.log(testobject);
        $("#forcastcontentrow").append("<div  class=\"col-xs-12\">\n" +
          `      <p class=\"text-left text-uppercase text-muted\">wk of ${testobject[i].week}</p>\n` +
          "    </div>\n" +
          "    <div class=\"col-xs-4 m-0 p-0\">\n" +
          "      <button class=\"btn btn-default btn-block m-0 p-0\"><span class=\"text-uppercase text-primary\">cbi</span></button>\n" +
          "    </div>\n" +
          "    <div class=\"col-xs-4 m-0 p-0\">\n" +
          "      <button class=\"btn btn-default btn-block m-0 p-0\"><span class=\"text-uppercase text-muted\">custom</span></button>\n" +
          "    </div>\n" +
          "    <div class=\"col-xs-4\">\n" +
          "      <form class=\"form-inline\">\n" +
          "        <input type=\"number\"\n" +
          "               class=\"form-control\"\n" +
          "               id=\"forcasteddeplietionsinput\">\n" +
          "      </form>\n" +
          "    </div>");
        document.getElementById('forcasteddeplietionsinput').value = testobject[i].number;

      } 

the document.getElementById('forcasteddeplietionsinput').value = testobject[i].number; document.getElementById('forcasteddeplietionsinput').value = testobject[i].number; is executed within the for loop so I am not sure why this is happening.在 for 循环中执行,所以我不确定为什么会这样。 What is wrong with my approach?我的方法有什么问题?

You need to change your selector for the text input element.您需要更改文本输入元素的选择器。

document.getElementById only returns the first element it finds in the DOM with the given id. document.getElementById只返回它在 DOM 中找到的具有给定 id 的第一个元素。 Since your for loop is duplicating that id, this line will only ever change the first text input element:由于您的 for 循环正在复制该 ID,因此该行只会更改第一个文本输入元素:

document.getElementById('forcasteddeplietionsinput').value = testobject[i].number;

What you could do is change this line:你可以做的是改变这一行:

id=\"forcasteddeplietionsinput\">\n"

...to this: ...对此:

class=\"forcasteddeplietionsinput\">\n"

...and then use getElementsByClassName to return an array of all the text inputs and index into it, like this: ...然后使用getElementsByClassName返回所有文本输入的数组并对其进行索引,如下所示:

document.getElementsByClassName('forcasteddeplietionsinput')[i].value = testobject[i].number;

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

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