2016-09-09 1 views
0

HTMLКак добавить выбранный атрибут в раскрывающийся список тегов в Meteor 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}} 

Но это, конечно, не представляется возможным сравнить переменную внутри, если блок Blaze.
Любая идея, как достичь желаемого результата?

+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'); 
Смежные вопросы