如何将选定的属性添加到Meteor Blaze中的下拉选项标记中
How to add selected attribute to dropdown option tag in Meteor Blaze?
HTML
<select id="article-weight">
{{#each weightValues}}
<option value="{{this}}">{{this}}</option>
{{/each}}
</select>
JS
Template.articleSingle.helpers({
weightValues: function(){
return [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
}
)};
Template.articleSingle.events({
'change #article-weight': function (event, template) {
weight = parseInt( $(event.currentTarget).val() );
Meteor.call('updateArticle', template.data._id, {
weight: weight
});
}
)};
我想要这种
{{#each weightValues}}
<option {{#if weight==this}}selected{{/if}} value="{{this}}">{{this}}</option>
{{/each}}
但肯定不可能比较Blaze if块中的变量
知道如何才能达到我想要的结果吗?
Template.articleSingle.helpers({
weightValues: function(){
return [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
},
isSelected:function(comparison){
return comparison === this;
},
yourComparison:function(){
return 3;
},
)};
{{#each weightValues}}
{{#if isSelected yourComparison}}
<option selected='true' value="{{this}}">{{this}}</option>
{{else}}
<option value="{{this}}">{{this}}</option>
{{/if}}
{{/each}}
尝试这个
Template.articleSingle.helpers({
weightValues(){
return [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
},
weight(){
return 2
},
)};
<select id="select">
{{#each weightValues}}
<option {{#if $eq this weight}} selected="selected" {{/if}} value="{{this}}">{{this}}</option>
{{/each}}
</select>
或者您可以使用javaScript
$('#select option[value="2"]').attr('selected', 'selected');
相关文章:
- 我的单元测试选项是什么
- 如何更改bigquery API中的计费层选项
- 使用Javascript获取所选选项ID
- Selectize.js:如何对整数值的选项进行排序
- 通过js在新选项卡中有条件地打开url
- 按照选项卡索引的顺序循环一个jQuery选择
- 如何在使用selectBoxIt JQuery插件时检测选项更改
- 无法在Ionic select中预先选择最后一个选项
- 如何在选项卡上定义属性'的主窗口对象
- 为什么grunt contrib connect的中间件选项的第三个参数是未定义的
- 使用此选项选择父类内部的类
- CKeditor:更改对话框中的默认选择选项
- JQuery覆盖不更改单选选项
- 活动选项卡's源代码-获取变量s值
- 绑定时将Parsley minlength消息作为选项传递时,未对其进行自定义
- 如何从选择框中的选项中获取属性值
- 如何在从多选下拉列表中选择选项值时动态生成文本框
- React Native通过Navigator将用户输入数据传递到选项卡栏IOS中的组件
- 如何从外部页面激活非默认引导选项卡
- 如何将选定的属性添加到Meteor Blaze中的下拉选项标记中