繁体   English   中英

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

[英]How to use jQuery to get Input Field Value of dynamic table

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

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

网站图片

在此处输入图片说明

部分代码

接收表单模板

<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>

接收行 2 模板

<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){
      ????
    })
  }
})

在第 4 行中,它在项目之前缺少一个 var,在第 6、7 行中也是如此。

在那之后,我认为你可以循环使用它们。(但它们将来自数据库)。

但是如果你想获得输入值,你为什么要在 MongoDB 中要求数据? 此外,我更愿意将 DB 值作为承诺。

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

并以这种方式取回项目:

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

暂无
暂无

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

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