如何在HBS中迭代数组

时间:2019-03-03 09:07:02

标签: javascript html handlebars.js

我有这种形式,其中我有2个输入(第3个被禁用)。每当用户更改第一输入的值时,我都想更改第二输入和第三输入的值。

我已通过hbs的阵列:

projectTasks: [  
{
  "taskId": "17-10-2018-1539717589999",
  "taskName": "create and plane of the path",
  "taskAvailable": 18.100000381469727
}, 
{
  "taskId": "17-10-2018-1539717624507",
  "taskName": "digging of road 3 inch",
  "taskAvailable": 420
  }
]

表单代码:-

   <div class="modal-body">
      <form method="POST" action="/assignSiteTask">
         <div class="form-group">
           <label for="recipient-name" class="control-label">Task Name</label>
           <select type="text" name="taskId" class="form-control" id="taskId" onchange="updateSiteTask()">
           {{#each projectTasks}}
               <option value="{{this.taskId}}">{{this.taskName}}</option>
           {{/each}}
           </select>
         </div>

         <div class="form-group">
           <label for="recipient-name" class="control-label">Count</label>
           <input type="text" name="taskCount" class="form-control" id="taskCount">
         </div>
         <div class="form-group">
         <label for="recipient-name" class="control-label">Avaiable Count</label>
         <input type="number" name="taskAvailable" class="form-control" id="taskAvailable" value="{{projectTasks.0.taskAvailable}}">
         </div>
       </div>
       <div class="modal-footer">
         <input value="Assign Task" type="submit" class="btn btn-primary"></input>
        </form>

在更改第一个输入的值时,将触发此功能:

function updateSiteTask(){
    let taskId = document.getElementById('taskId').value;
    var i=0
    for (i = 0; i < {{projectTasks.length}}; i++) { 
        let y = "{{projectTasks.i.taskId}}";
        if (  y === taskId )
        {
            break;
        }
    let taskAvailable = {{projectTasks.i.taskAvailable}};
    document.getElementById("taskAvailable").value = taskAvailable;
  }

}

0 个答案:

没有答案