【发布时间】:2019-01-05 12:29:39
【问题描述】:
我需要车把助手方面的帮助:
从数据库中选择的数据:
{ _id: 5b598e31f24e7f820d2d422d, key: 'c-1', name: 'C#' },
{ _id: 5b598e61f24e7f820d2d4234, key: 'node-js', name: 'Node.js'}
HBS 生成动态选择选项:
{{#each tags}}
<option value="{{_id}}">{{name}}</option>
{{/each}}
HTML:
<select multiple="" class="custom-select" name="skills">
<option value="5b598e22f24e7f820d2d422b">Javascript</option>
<option value="5b598e29f24e7f820d2d422c">C++</option>
<option value="5b598e31f24e7f820d2d422d">C#</option>
<option value="5b598e61f24e7f820d2d4234">Node.js</option>
</select>
如何将selected="selected"添加到具有相同ID的选项中?有什么优雅的解决方案吗?
对于单选字段,我使用代码:
{{#select post.position.id}}
{{#each categories}}
<option value="{{id}}">{{name}}</option>
{{/each}}
{{/select}}
helpers.js
_helpers.select = function(selected, options){
return options.fn(this).replace(new RegExp(' value=\"'+ selected + '\"'), '$&selected="selected"');
}
【问题讨论】:
标签: javascript html select handlebars.js helpers