Javascript 从Meteor js中的每一行获取值
有一张用模板包装的桌子。我要做的是从这个表的指定字段中获取值,以及表单中的值。每行都有一个按钮来传递值。但当我试图在控制台中打印它们时,它说它是未定义的。 因此,任何人都有解决这个问题的方法或更好的方法,我将非常感激 到目前为止,我已经尝试使用Javascript 从Meteor js中的每一行获取值,javascript,jquery,meteor,meteor-blaze,Javascript,Jquery,Meteor,Meteor Blaze,有一张用模板包装的桌子。我要做的是从这个表的指定字段中获取值,以及表单中的值。每行都有一个按钮来传递值。但当我试图在控制台中打印它们时,它说它是未定义的。 因此,任何人都有解决这个问题的方法或更好的方法,我将非常感激 到目前为止,我已经尝试使用serializeArray()。然而,它似乎无法从元素中获取值 <template name="student"> {{#each student}} <tr> <td>{{name}}&l
serializeArray()
。然而,它似乎无法从
元素中获取值
<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>
我与Blaze一起使用,它使这种方式变得超级简单,但如果没有它,您可以:
在输入中添加一个数据studendid
<td><input type="submit" class="btn btn-primary markAttendance" data-studentid="{{studentID}}"></td>
然后,您可以在方法处理程序中按ID查询学生,即:
Students.findOne({u ID:studentId})
还有一个问题,如果要在该模板的文本区域中获取值,该如何做。我只能从第一行获得值$(“[name=备注]”).val()`如果将click事件处理程序移动到学生模板,则可以执行instance.find('textarea').val()来获取值。您的另一个选项是给该textarea一个id aa,然后给$(“#mytextareaid”).val();
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);
//.....
}
});
<td><input type="submit" class="btn btn-primary markAttendance" data-studentid="{{studentID}}"></td>