将州/省选择元素添加到你的 Meteor 应用






4.47/5 (5投票s)
可复制粘贴的 HTML 和 JS 代码,为你的 Meteor 应用提供州/省选择选项
流星不及山峦
我们经常在网站/应用中看到 Select 元素,其选项包含美国的所有州。当然,根据你居住地和/或目标受众,你可能希望用不同的值填充 Select 元素,但对于服务于美国市场的应用,以下代码片段可以完全“原样”地用于 Meteor 应用,唯一的例外是,你需要将模板名称(以下代码片段中的“addJobLoc”)替换为你自己的模板名称。
作为额外的奖励(而且是免费的!),我也添加了加拿大的省份和地区。毕竟,我们怎么能忘记、忽视或“贬低”我们北方的邻居呢?他们给了我们尼尔·杨、戈登·莱特福特、The Guess Who、Rush、布莱恩·亚当斯、安妮·穆雷、The Band 中的大多数成员、J. Kenneth Galbraith、弗格森·詹金斯、史蒂夫·纳什、“Strange Brew”、枫糖浆等等。
首先,将以下 HTML 和 SpaceBars 代码添加到你的模板的 .html 文件中
<select id="stateorprovince" name="stateorprovince"> {{#each statesAndProvinces}} <option title="{{hint}}">{{abbrcode}}</option> {{/each}} </select>
请注意,我使用完整的地名作为该项的“悬停提示”,并使用双字母缩写作为内容(选择选项)。
现在将此代码放入模板对应的 *.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" }] } });
请注意,我从 wholypantalones(很棒的昵称,一流的!)这里获得了州和省份的 JSON 列表。
闪耀吧,打蜡平台(框架?)!
在超过二十年的编程生涯中,我大多容忍(有时勉强容忍)我所使用的特定技术(其中一些(我们暂且不说,但与“Hacktuate”、“Mistr[i]al Reports”和“Windoze Bomb-packed Framework”押韵)工作起来痛苦到我认真考虑过转行)。
然而,有两次,我彻底“爱上”了特定的技术:第一次是 Delphi(Object Pascal),IMO,它在所有方面都超越了它的同行,除了市场份额和一些负责定位这种优秀的语言和工具组合的人的商业头脑。第二次(现在)是 Meteor,一个基于 node.js 的、同构 JavaScript 盛宴。
Meteor 是一个正在兴起的(而不是衰落的 - 与冲浪板或皮肤打蜡无关)平台(或框架,取决于你的观点),如果你还没有尝试过,你欠自己一个,用不可思议且不可压抑的印第安纳州人约翰·梅伦坎普的话来说,去看看吧。