簡體   English   中英

如何從 Meteor.js 中的指定表行中獲取值

[英]How to get values from specified table rows in Meteor.js

在我的 Meteor 應用程序中,我有下表顯示了一些信息。 用戶可以編輯公司名稱、地址和 email 並單擊旁邊的按鈕保存更改。

<table class="table table-hover">
  <thead>
    <tr>
      <th>Company Name</th>
      <th>Company Address</th>
      <th>Email Address</th>
      <th>Last Login</th>
      <th>Manage</th>
    </tr>
  </thead>
  <tbody>
    {{#each users}}
    <tr>
      <td><input type="text" name="companyName" value="{{companyName}}"></td>
      <td><input type="text" name="companyAddress" value="{{companyAddress}}"</td>
      <td><input type="text" name="companyEmail" value="{{profile.email}}"></td>
      <td>{{date}}</td>
      <td>
        <input type="submit" class="btn btn-primary save" value="Save">
        <input type="submit" class="btn btn-danger delete" value="Delete">
      </td>
    </tr>
    {{/each}}
  </tbody>
</table>

在事件處理程序中:

'click .save':function(e,inst){
  var userID = this._id;
  var companyName = inst.$('[name=companyName]').val();
  var companyAddress = inst.$('[name=companyAddress]').val();
  var companyEmail = inst.$('[name=companyEmail]').val();

  console.log(companyName);
  console.log(userID);
  console.log(companyAddress);
  console.log(companyEmail);
}

這樣,它只能讀取表的第一行的值。 然后我在輸入字段中添加 data-id <td><input type="text" name="companyName" value="{{companyName}}" data-id="{{_id}}"></td>它能夠從表格行中獲取公司名稱。 我的問題是如何獲得rest的屬性。

您需要為所有輸入字段填充 id,我通常這樣使用

//in html    
<input type="text" name="companyName{{_id}}" value="{{companyName}}">

//in helper
var companyName = inst.$(`[name=companyName${this._id}]`).val();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM