admin管理员组文章数量:1241063
I'm trying to render a select element with options, like this
<select dropdown multiple class="dropdown" name="color">
{{#each colors}}
<option value="{{value}}" {{isSelected parentColor id}}>{{title}}></option>
{{/each}}
</select>
I'm using the following handlebars helper
Handlebars.registerHelper('isSelected', function(input, color) {
return input === color ? 'selected' : '';
});
The problem is that the selected
attribute doesn't show up on any of the option
element, but when I place a console.log
in the handlebar helper I do see that one matches (input === color === true). Any ideas what I do wrong here ?
I'm trying to render a select element with options, like this
<select dropdown multiple class="dropdown" name="color">
{{#each colors}}
<option value="{{value}}" {{isSelected parentColor id}}>{{title}}></option>
{{/each}}
</select>
I'm using the following handlebars helper
Handlebars.registerHelper('isSelected', function(input, color) {
return input === color ? 'selected' : '';
});
The problem is that the selected
attribute doesn't show up on any of the option
element, but when I place a console.log
in the handlebar helper I do see that one matches (input === color === true). Any ideas what I do wrong here ?
1 Answer
Reset to default 14This is a working example of what is described,
http://jsfiddle/rtLGR/
hbs
<h1>Handlebars JS Example</h1>
<script id="some-template" type="text/x-handlebars-template">
<select dropdown multiple class="dropdown" name="color">
{{#each colors}}
<option value="{{value}}" {{isSelected parentColor id}}>{{title}}</option>
{{/each}}
</select>
</script>
js
var source = $("#some-template").html();
var template = Handlebars.pile(source);
var data = {
colors: [
{id:1,value:1,title:"red",parentColor:2},
{id:2,value:2,title:"green",parentColor:2},
{id:3,value:3,title:"blue",parentColor:1}
]
};
Handlebars.registerHelper('isSelected', function (input, color) {
return input === color ? 'selected' : '';
});
$('body').append(template(data));
本文标签: javascripthandlebars conditionally add attributeStack Overflow
版权声明:本文标题:javascript - handlebars: conditionally add attribute - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740089607a2223925.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论