2016-09-09 5 views
0

HTMLMeteor Blazeのドロップダウンオプションタグに選択した属性を追加するには?

<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}} 

ような何かをしたいしかし、それはブレイズの場合はブロック内の変数を比較することは確かに可能ではありません。
どのように私の希望の結果を達成することができますか?

+0

を行うことができますが平等ヘルパーを作成する方法のためhttp://stackoverflow.com/a/30475012/2805154を参照してください。 –

答えて

0
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}} 
0

この

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'); 
関連する問題