因此,我试图在编辑表单上设置单选按钮的值。我尝试使用jQuery运行if语句,然后使用选择器设置checked属性。但是我想我不了解jQuery和Template.template.helpers如何一起工作,因为我永远无法让jQuery做任何事情。
无论如何,我有以下工作要做,但是如果这是最好的方法,我只是好奇。就像在表单中设置单选按钮的值一样简单,这似乎有点肿。我想可能会有更好地适用于表格的软件包。
这是Javascript
Template.clientEdit.helpers({
// set the radio buttons
isActive: function() {
if (this.status == "active") {
return "checked";
}
},
isArchived: function() {
if (this.status == "archived") {
return "checked";
}
},
});
和HTML
<input type="radio" name="status" id="status-radio-1" value="active" {{isActive}} >
<input type="radio" name="status" id="status-radio-2" value="archived" {{isArchived}} >
谢谢
您可以编写一个全局把手帮助程序,以在您所有的Meteor表单中使用,例如:
Handlebars.registerHelper('checked_eq', function(x, y){
if (x === y){
return ' checked="checked"';
}
return ''
})
并在您的模板中使用它,例如:
<input type="radio" name="status" id="status-radio-1" value="active" {{checked_eq status 'active'}} >
<input type="radio" name="status" id="status-radio-2" value="archived" {{checked_eq status 'archived'}} >
您还可以编写一些更通用的东西,例如:
Handlebars.registerHelper 'checked', (x)-> if x then ' checked="checked"' else ''
Handlebars.registerHelper 'notChecked', (x)-> if x then '' else ' checked="checked"'
# repeat for disabled / selected etc
并在渲染模板之前对您的data / dataContext也应用转换:
// ... using iron-router for data contet
data: function(){
var client = Clients.findOne(this.params.clientId);
client.checkedActive = (client.status == 'active');
client.checkedArchived = (client.status == 'archived');
return client;
}
// ...
// ... or apply a transform when making a list
Template.clientList.helpers({
clients: function(){
return Clients.find({},{
transform: function(client){
client.checkedActive = (client.status == 'active');
client.checkedArchived = (client.status == 'archived');
return client;
}
});
}
});
然后在您的模板中使用,例如:
<input type="radio" name="status" id="status-radio-1" value="active" {{checked checkedActive}} >
<input type="radio" name="status" id="status-radio-2" value="archived" {{checked checkedArchived}} >
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句