繁体   English   中英

如何在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');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM