简体   繁体   English

如何使用Json对象填充动态添加的输入字段

[英]How to populate dynamically added input fields with Json object

I have a this table of input fields which I can add rows dynamically with help of KnockoutJS 我有一个输入字段表,我可以在KnockoutJS的帮助下动态添加行

     <table id="sTypeTable" class="table table-bordered" hidden="hidden">
         <tr>
             <th> Name </th>
             <th> Value </th>
             <th> <a class="btn btn-primary btn-sm"  onclick="addReqField(0);"><i class="fa fa-plus"></i></a></th>
         </tr>
<!-- ko foreach: {data: requestFields, as: 'reqField'} -->
        <tr id="sRow">
           <td>
              <input id="sName" data-bind="value: reqField[0]" onblur="createJSON()"/>
           </td>
           <td>
              <input id="sValue" data-bind="value: reqField[0]" onblur="createJSON()"/>
           </td>
           <td>
              <a class="btn btn-warning  btn-sm" data-bind="click: removeResField2" ><i class="glyphicon glyphicon-remove"></i></a>
          </td>
       </tr>
<!-- /ko -->
 </table>
  <div class="col-md-11 ">
    <textarea style="font-size: larger; min-height: 200px" class="form-control" id="requestData" oninput="storeValueOfTextArea()"></textarea>
  </div>

From this inputs I'm generating following Json object on textarea (id="requestData") 从这个输入我在textarea上生成以下Json对象(id =“requestData”)

[
  {
    "users": [
      {
        "name": "John",
        "value": "12"
      },
      {
        "name": "Sarah",
        "value": "13"
      },
      {
        "name": "Tom",
        "value": "14"
      }
    ]
  }
]

It works well, but now I need populate these input fields from Json object, When object entered to textarea. 它运行良好,但现在我需要从Json对象填充这些输入字段,当对象输入到textarea时。 I have tried following way 我试过以下方式

<script th:inline="javascript">

 function storeValueOfTextArea() {
      var lines = $('#requestData').val();
      var texts = JSON.parse(lines);

  for (var i=0; i!== texts[0].users.length;i++){
      addReqField();  // method for adding new row of input fields

      let  v = texts[0].users[i];
      $("tr[id=sRow]").each(function() {
          $("#sName").val(v.name);
          $("#sValue").val(v.value);
      });
  }
 </script>

but in result only first input field gets the last object 但结果只有第一个输入字段获取最后一个对象

{
   "name": "Tom",
   "value": "14"
 }

others stays empty 其他人保持空虚

$("tr[id=sRow]").each(function() {
      $("#sName").val(v.name);
      $("#sValue").val(v.value);
  });

This doesn't seem to be good. 这似乎不太好。 You should get the row index you want, according to the index of the user for which data you want to fill. 您应该根据要填充数据的用户的索引获取所需的行索引。 Right now what this does is to fill every row with the same user data, in all iterations of the for loop. 现在这样做是为了在for循环的所有迭代中用相同的用户数据填充每一行。 In the end, the last user data will be in all rows. 最后,最后一个用户数据将在所有行中。

I think the problem is that you're giving each created new row the same ID of sRow . 我认为问题在于你为每个创建的新行提供了相同的sRow ID。

So inside your loop 在你的循环中

  $("tr[id=sRow]").each(function() {
      $("#sName").val(v.name);
      $("#sValue").val(v.value);
  });

in the last iteration this will fill all sRow elements with the objects last values of Tom and 14 . 在最后一次迭代中,这将填充所有sRow元素,其中对象的最后值为Tom14 Try giving your rows an unique id like sRow + a counter from 0 onwards sRow 0 sRow 1 ... 尝试给你的行一个唯一的id,如sRow +一个从0开始的计数器sRow 0 sRow 1 ...

Thank you all who have answered to this question. 谢谢所有回答过这个问题的人。 I have solved this problem by this way 我通过这种方式解决了这个问题

 for (let i=0; i!== texts[0].values.length;i++){
      addReqField();
      let  v = texts[0].values[i];
          $("tr[id=sRow]").each(function(index) {
              if (index === i){
                 $(this).find("#sName").val(v.name);
                 $(this).find("#sValue").val(v.value);
               }

           });
 }

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

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