如何使用jQuery获取动态表的输入字段值

时间:2017-09-13 08:44:43

标签: javascript jquery meteor meteor-blaze

我目前正在使用 Meteor / Blaze框架来构建表格。表格中的行将根据所选供应商而变化。我已将class={{_id}}添加到字段,q.{{_id}}lot.{{_id}}exp.{{_id}}添加到数量,批次和过期日期 INPUT

我正在尝试编写提交事件以获取这些值并将其传递给 Mongo 数据库。请建议一个循环遍历这些行的好方法来获取值。

网站图片

enter image description here

部分代码

receiveForm模板

<template name="receiveForm">
...
<select data-placeholder="Select an option" class="sel2js form-control select select-primary" id="supplier_sel" name="supplier">
      {{#each suppliers}}
        {{> sel_supplier}}
      {{/each}}
    </select>
  </div>
  <!-- Receive Lot Table -->
  <table class="table table-bordered table-hover">
    <thead>
      <tr>
        <th>Product Name</th>
        <th>Current Quantity</th>
        <th>Unit of Measurement</th>
        <th>Receive Quantity</th>
        <th>Lot No</th>
        <th>Exp Date (DD/MM/YYYY)</th>
      </tr>
    </thead>
    <tbody>
      {{#each items}}
        {{> receiveRow2}}
      {{/each}}
    </tbody>
</table>
<div class="text-center">
  <button type="submit" class="btn btn-embossed btn-primary btn-wide" id="submitNewReceive" value="Submit">Submit</button>
  <button type="reset" class="btn btn-embossed btn-warning btn-wide" value="Reset">Reset</button>
</div>
</form>

receiveRow2模板

<template name="receiveRow2">
  <tr id="{{_id}}">
    <td class="pn">{{name}}</td>
    <td class="pq">{{totalQuantity}}</td>
    <td>{{uomid.name}} ({{uomid.unit}} {{uomid.unitname}}/{{uomid.name}})</td>
    <td><input type="text" class="form-control" name="q.{{_id}}" placeholder="Quantity" /></td>
    <td><input type="text" class="form-control" name="lot.{{_id}}" placeholder="Lot No XX/YYYY" /></td>
    <td>
      <div class="input-group datetimepicker text-primary">
        <span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
        <input class="set-due-date form-control" name="exp.{{_id}}" type="text" placeholder="วัน/เดือน/ปี"/>
        <hr />
      </div>
    </td>
  </tr>
</template>

JS

Template.receiveForm.events({
  'submit form': function(event, template){
    var supplierSelected = Template.instance().supplierSelected;
    items = Products.find({suppliers: supplierSelected.get()});
    event.preventDefault();
    docdate = event.target.docdate.value;
    supplier = event.target.supplier_sel.value;
    console.log("---event---");
    console.log(docdate)
    console.log(supplier)
    items.forEach(function(item){
      ????
    })
  }
})

1 个答案:

答案 0 :(得分:3)

在第4行中它缺少一个var之前的项目, 还有第6,7行。 在那之后,我认为你可以循环它们。(但它们将来自数据库) 但是如果你想获得输入值,为什么要在MongoDB中索取数据呢? 此外,我会更愿意将DB值作为承诺。

var items = Promise.resolve(Products.find({suppliers: supplierSelected.get()});

以这种方式取回物品:

items.then(function(item){console.log(item)})