2015-02-15 7 views
6

मेटीर में ड्रॉपडाउन सूची (और रेडियो में) से मूल्यों को प्राप्त करने और चुनने का सबसे अच्छा तरीका क्या है। मैं एक सहायक बनाया है:उल्का ड्रॉपडाउन सूची

Template.categories.helpers({ 
    categories: ["facebook", "news", "tv", "tweets"] 
}); 

और html में

... 
<select class="form-control" id="category"> 
    {{> categories}} 
</select> 
... 
<template name="categories"> 
    <option disabled="disabled" selected="selected">Please Select</option> 
    {{#each categories}} 
     <option value="{{this}}">{{this}}</option> 
    {{/each}} 
</template> 

संपादित के मामले में, मैं मूल्य डेटाबेस से आ रही है (जैसे समाचार) का चयन किया जाना साथ यह मूल्यांकन करना चाहते हैं।

अग्रिम धन्यवाद।

+0

मैं क्या आप डेटाबेस से लौटे किया जा रहा है के बारे में विवरण जानने के बिना सबसे अच्छा मैं कर सकते हैं जवाब दे दिया है। क्या आप हमें अधिक जानकारी दे सकते हैं? आपको आमतौर पर क्या करना है प्रत्येक विकल्प में एक अतिरिक्त सहायक है जो दो मानों की तुलना करेगा; यदि वे मिलते हैं तो आप "चयनित" लौटते हैं। –

उत्तर

9

टेम्पलेट HTML:

<select id="category-select"> 
    <option disabled="disabled" selected="selected">Please Select</option> 
    {{#each categories}} 
     <option value="{{this}}">{{this}}</option> 
    {{/each}} 
</select> 

खाका js:

Template.categories.helpers({ 
    categories: function(){ 
     return ["facebook", "news", "tv", "tweets"] 
    } 
}); 

Template.categories.events({ 
    "change #category-select": function (event, template) { 
     var category = $(event.currentTarget).val(); 
     console.log("category : " + category); 
     // additional code to do what you want with the category 
    } 
}); 
1
Template.categories.helpers({ 
    categories: function(){ 
     return ["facebook", "news", "tv", "tweets"] 
    } 
}); 

और आपको टेम्पलेट नाम और सहायक बदलने के बारे में विचार करना चाहिए, वे समान नहीं होना चाहिए।

संबंधित मुद्दे