我以为我可以返回HTML select元素的选项:
在模板的.HTML文件中:
<select id="stateorprovince" name="stateorprovince">
{{statesAndProvinces}}
</select>
在模板的.js文件中(目前虚假数据;至少会取代美国和加拿大省):
Template.addJobLoc.helpers({
statesAndProvinces: function() {
return
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
}
});
...但是它无法在第一个“选项”
的开头用语法错误构建如果我将选项括在单引号中,则错误为“Unexpected token ILLEGAL”
完成此任务的正确方法是什么?
答案 0 :(得分:2)
您可以使用这种方法,这比从模板助手返回HTML更合适:
HTML
<template name="addJobLoc">
<select id="stateorprovince" name="stateorprovince">
{{#each statesAndProvinces}}
<option value="{{value}}">{{label}}</option>
{{/each}}
</select>
</template>
JS
Template.addJobLoc.helpers({
statesAndProvinces: function(){
return [{
value: "volvo",
label: "Volvo"
},{
value: "saab",
label: "Saab"
}, ...];
}
});
为了简单起见,我们在帮助器中返回一个硬编码数组,但您也可以返回收集数据。
答案 1 :(得分:1)
在您的助手中,您需要将返回值包装在引号中。
我建议进行以下更改
助手
statesAndProvinces: function(){
return ["Volvo", "Saab","Mercedes"];
}
HTML(在选择标签内)
{{#each statesAndProvinces}}
<option value="{{this}}">{{this}}</option>
{{/each}}
答案 2 :(得分:0)
调整saimeunt的接受答案,这就是我最终的结果(只是为了显示完整的代码)。
如果您想要美国各州(和地区等)以及加拿大省,您可以按原样在Meteor应用中使用此功能,除了替换&#34; addJobLoc&#34;的模板名称。使用您的模板名称。
我使用完整的地名作为&#34;悬停提示&#34;对于项目,以及两个字母的指定作为内容(选择选项)。
我从wholypantalones获得了州和省的JSON列表(伟大的绰号,primo!)here。
HTML:
<select id="stateorprovince" name="stateorprovince">
{{#each statesAndProvinces}}
<option title="{{hint}}">{{abbrcode}}</option>
{{/each}}
</select>
JavaScript(* .js文件中的帮助程序):
Template.addJobLoc.helpers({
statesAndProvinces: function() {
return [{
"hint": "Alabama",
"abbrcode": "AL"
}, {
"hint": "Alaska",
"abbrcode": "AK"
}, {
"hint": "American Samoa",
"abbrcode": "AS"
}, {
"hint": "Arizona",
"abbrcode": "AZ"
}, {
"hint": "Arkansas",
"abbrcode": "AR"
}, {
"hint": "British Columbia",
"abbrcode": "BC"
}, {
"hint": "California",
"abbrcode": "CA"
}, {
"hint": "Colorado",
"abbrcode": "CO"
}, {
"hint": "Connecticut",
"abbrcode": "CT"
}, {
"hint": "Delaware",
"abbrcode": "DE"
}, {
"hint": "District Of Columbia",
"abbrcode": "DC"
}, {
"hint": "Federated States Of Micronesia",
"abbrcode": "FM"
}, {
"hint": "Florida",
"abbrcode": "FL"
}, {
"hint": "Georgia",
"abbrcode": "GA"
}, {
"hint": "Guam",
"abbrcode": "GU"
}, {
"hint": "Hawaii",
"abbrcode": "HI"
}, {
"hint": "Idaho",
"abbrcode": "ID"
}, {
"hint": "Illinois",
"abbrcode": "IL"
}, {
"hint": "Indiana",
"abbrcode": "IN"
}, {
"hint": "Iowa",
"abbrcode": "IA"
}, {
"hint": "Kansas",
"abbrcode": "KS"
}, {
"hint": "Kentucky",
"abbrcode": "KY"
}, {
"hint": "Louisiana",
"abbrcode": "LA"
}, {
"hint": "Maine",
"abbrcode": "ME"
}, {
"hint": "Manitoba",
"abbrcode": "MB"
}, {
"hint": "Marshall Islands",
"abbrcode": "MH"
}, {
"hint": "Maryland",
"abbrcode": "MD"
}, {
"hint": "Massachusetts",
"abbrcode": "MA"
}, {
"hint": "Michigan",
"abbrcode": "MI"
}, {
"hint": "Minnesota",
"abbrcode": "MN"
}, {
"hint": "Mississippi",
"abbrcode": "MS"
}, {
"hint": "Missouri",
"abbrcode": "MO"
}, {
"hint": "Montana",
"abbrcode": "MT"
}, {
"hint": "Nebraska",
"abbrcode": "NE"
}, {
"hint": "Nevada",
"abbrcode": "NV"
}, {
"hint": "New Brunswick",
"abbrcode": "NB"
}, {
"hint": "New Hampshire",
"abbrcode": "NH"
}, {
"hint": "New Jersey",
"abbrcode": "NJ"
}, {
"hint": "New Mexico",
"abbrcode": "NM"
}, {
"hint": "New York",
"abbrcode": "NY"
}, {
"hint": "Newfoundland and Labrador",
"abbrcode": "NL"
}, {
"hint": "North Carolina",
"abbrcode": "NC"
}, {
"hint": "North Dakota",
"abbrcode": "ND"
}, {
"hint": "Northern Mariana Islands",
"abbrcode": "MP"
}, {
"hint": "Nova Scotia",
"abbrcode": "NS"
}, {
"hint": "Northwest Territories",
"abbrcode": "NT"
}, {
"hint": "Nunavut",
"abbrcode": "NU"
}, {
"hint": "Ohio",
"abbrcode": "OH"
}, {
"hint": "Oklahoma",
"abbrcode": "OK"
}, {
"hint": "Ontario",
"abbrcode": "ON"
}, {
"hint": "Oregon",
"abbrcode": "OR"
}, {
"hint": "Palau",
"abbrcode": "PW"
}, {
"hint": "Pennsylvania",
"abbrcode": "PA"
}, {
"hint": "Prince Edward Island",
"abbrcode": "PE"
}, {
"hint": "Puerto Rico",
"abbrcode": "PR"
}, {
"hint": "Quebec",
"abbrcode": "QC"
}, {
"hint": "Rhode Island",
"abbrcode": "RI"
}, {
"hint": "Saskatchewan",
"abbrcode": "SK"
}, {
"hint": "South Carolina",
"abbrcode": "SC"
}, {
"hint": "South Dakota",
"abbrcode": "SD"
}, {
"hint": "Tennessee",
"abbrcode": "TN"
}, {
"hint": "Texas",
"abbrcode": "TX"
}, {
"hint": "Utah",
"abbrcode": "UT"
}, {
"hint": "Vermont",
"abbrcode": "VT"
}, {
"hint": "Virgin Islands",
"abbrcode": "VI"
}, {
"hint": "Virginia",
"abbrcode": "VA"
}, {
"hint": "Washington",
"abbrcode": "WA"
}, {
"hint": "West Virginia",
"abbrcode": "WV"
}, {
"hint": "Wisconsin",
"abbrcode": "WI"
}, {
"hint": "Wyoming",
"abbrcode": "WY"
}, {
"hint": "Yukon",
"abbrcode": "YT"
}]
}
});