从 Meteor js 中的每一行获取值

Getting values from each row in Meteor js

有一个 table 被模板包裹。我想要做的是从这个 table 的指定字段中获取值以及表单中的值。每行都有一个按钮来传递值。但是当我试图在控制台中打印它们时,它说它是未定义的。 所以如果有人对此有解决方案或更好的方法,我将不胜感激。

到目前为止,我已经尝试使用 serializeArray()。但是,它似乎无法从 <td> 元素中获取值。

<template name="student">
    {{#each student}}
    <tr>
      <td>{{name}}</td>
      <td>{{studentID}}</td>
      <td><textarea rows="1"></textarea></td>
      <td><input type="submit" class="btn btn-primary markAttendance"></td>
    </tr>
    {{/each}}
</template>

<template name="subject">
    <select id="dropdown" name="dropdown" class="form-control">
    <option>Please select a subject</option>
    {{#each subject}}
    <option>{{subjectCode}}</option>
    {{/each}}
    </select>
</template>

事件处理程序:

Template.content.events({
   'click .markAttendance':function(e){
     e.preventDefault();
     var subjectCode = $(e.target).find('[name=dropdown]').val();
     var week = $(e.target).find('[name=week]').val();
     var studentName = $(e.target).find('td:eq(0)').text();
     var studentID = $(e.target).find('td:eq(1)').text();
     console.log(subjectCode);
     console.log(week);
     console.log(studentName);
     console.log(studentID);
     //.....
   }
 });
    Template.content.events({
      'click .markAttendance':function(event, instance){
       e.preventDefault(); // not needed unless you have a form someplace I'm not seeing
       var student = this;
       var subjectCode = $('#dropdown').val();
       var week = $('[name=week]').val();
       var studentName = student.name;
       var studentID = student._id;
       console.log(subjectCode);
       console.log(week);
       console.log(studentName);
       console.log(studentID);
     //.....
   }
});

我将 https://viewmodelblaze.azurewebsites.net/blaze 与 Blaze 一起使用,它使这种超级简单,但如果没有它,您可以:

在输入中添加一个data-studendid

<td><input type="submit" class="btn btn-primary markAttendance" data-studentid="{{studentID}}"></td>

然后您可以在您的方法处理程序中通过 ID 查询学生,即:Students.findOne({_id: studentId})