一、需求:
提供省、市下拉選單,當使用者選擇省一級下拉選單項後,市下拉選單的各個選項自動變為該省對應的城市列表。
二、效果:
三、實現:
1.省市json資料,來自:
http://www.cnblogs.com/jinzhao/archive/2012/04/01/2428142.html
- /**
- * 省市json資料
- */
- var provinceAndCityArray = [{ name: "北京", cities: ["西城", "東城", "崇文", "宣武", "朝陽", "海淀", "豐臺", "石景山", "門頭溝", "房山", "通州", "順義", "大興", "昌平", "平谷", "懷柔", "密雲", "延慶"] },
- { name: "天津", cities: ["青羊", "河東", "河西", "南開", "河北", "紅橋", "塘沽", "漢沽", "大港", "東麗", "西青", "北辰", "津南", "武清", "寶坻", "靜海", "寧河", "薊縣", "開發區"] },
- { name: "河北", cities: ["石家莊", "秦皇島", "廊坊", "保定", "邯鄲", "唐山", "邢臺", "衡水", "張家口", "承德", "滄州", "衡水"] },
- { name: "山西", cities: ["太原", "大同", "長治", "晉中", "陽泉", "朔州", "運城", "臨汾"] },
- { name: "內蒙古", cities: ["呼和浩特", "赤峰", "通遼", "錫林郭勒", "興安"] },
- { name: "遼寧", cities: ["大連", "瀋陽", "鞍山", "撫順", "營口", "錦州", "丹東", "朝陽", "遼陽", "阜新", "鐵嶺", "盤錦", "本溪", "葫蘆島"] },
- { name: "吉林", cities: ["長春", "吉林", "四平", "遼源", "通化", "延吉", "白城", "遼源", "松原", "臨江", "琿春"] },
- { name: "黑龍江", cities: ["哈爾濱", "齊齊哈爾", "大慶", "牡丹江", "鶴崗", "佳木斯", "綏化"] },
- { name: "上海", cities: ["浦東", "楊浦", "徐匯", "靜安", "盧灣", "黃浦", "普陀", "閘北", "虹口", "長寧", "寶山", "閔行", "嘉定", "金山", "松江", "青浦", "崇明", "奉賢", "南匯"] },
- { name: "江蘇", cities: ["南京", "蘇州", "無錫", "常州", "揚州", "徐州", "南通", "鎮江", "泰州", "淮安", "連雲港", "宿遷", "鹽城", "淮陰", "沐陽", "張家港"] },
- { name: "浙江", cities: ["杭州", "金華", "寧波", "溫州", "嘉興", "紹興", "麗水", "湖州", "台州", "舟山", "衢州"] },
- { name: "安徽", cities: ["合肥", "馬鞍山", "蚌埠", "黃山", "蕪湖", "淮南", "銅陵", "阜陽", "宣城", "安慶"] },
- { name: "福建", cities: ["福州", "廈門", "泉州", "漳州", "南平", "龍巖", "莆田", "三明", "寧德"] },
- { name: "江西", cities: ["南昌", "景德鎮", "上饒", "萍鄉", "九江", "吉安", "宜春", "鷹潭", "新餘", "贛州"] },
- { name: "山東", cities: ["青島", "濟南", "淄博", "煙臺", "泰安", "臨沂", "日照", "德州", "威海", "東營", "荷澤", "濟寧", "濰坊", "棗莊", "聊城"] },
- { name: "河南", cities: ["鄭州", "洛陽", "開封", "平頂山", "濮陽", "安陽", "許昌", "南陽", "信陽", "周口", "新鄉", "焦作", "三門峽", "商丘"] },
- { name: "湖北", cities: ["武漢", "襄樊", "孝感", "十堰", "荊州", "黃石", "宜昌", "黃岡", "恩施", "鄂州", "江漢", "隨棗", "荊沙", "咸寧"] },
- { name: "湖南", cities: ["長沙", "湘潭", "岳陽", "株洲", "懷化", "永州", "益陽", "張家界", "常德", "衡陽", "湘西", "邵陽", "婁底", "郴州"] },
- { name: "廣東", cities: ["廣州", "深圳", "東莞", "佛山", "珠海", "汕頭", "韶關", "江門", "梅州", "揭陽", "中山", "河源", "惠州", "茂名", "湛江", "陽江", "潮州", "雲浮", "汕尾", "潮陽", "肇慶", "順德", "清遠"] },
- { name: "廣西", cities: ["南寧", "桂林", "柳州", "梧州", "來賓", "貴港", "玉林", "賀州"] },
- { name: "海南", cities: ["海口", "三亞"] },
- { name: "重慶", cities: ["渝中", "大渡口", "江北", "沙坪壩", "九龍坡", "南岸", "北碚", "萬盛", "雙橋", "渝北", "巴南", "萬州", "涪陵", "黔江", "長壽"] },
- { name: "四川", cities: ["成都", "達州", "南充", "樂山", "綿陽", "德陽", "內江", "遂寧", "宜賓", "巴中", "自貢", "康定", "攀枝花"] },
- { name: "貴州", cities: ["貴陽", "遵義", "安順", "黔西南", "都勻"] },
- { name: "雲南", cities: ["昆明", "麗江", "昭通", "玉溪", "臨滄", "文山", "紅河", "楚雄", "大理"] },
- { name: "西藏", cities: ["拉薩", "林芝", "日喀則", "昌都"] },
- { name: "陝西", cities: ["西安", "咸陽", "延安", "漢中", "榆林", "商南", "略陽", "宜君", "麟遊", "白河"] },
- { name: "甘肅", cities: ["蘭州", "金昌", "天水", "武威", "張掖", "平涼", "酒泉"] },
- { name: "青海", cities: ["黃南", "海南", "西寧", "海東", "海西", "海北", "果洛", "玉樹"] },
- { name: "寧夏", cities: ["銀川", "吳忠"] },
- { name: "新疆", cities: ["烏魯木齊", "哈密", "喀什", "巴音郭楞", "昌吉", "伊犁", "阿勒泰", "克拉瑪依", "博爾塔拉"] },
- { name: "香港", cities: ["中西區", "灣仔區", "東區", "南區", "九龍-油尖旺區", "九龍-深水埗區", "九龍-九龍城區", "九龍-黃大仙區", "九龍-觀塘區", "新界-北區", "新界-大埔區", "新界-沙田區", "新界-西貢區", "新界-荃灣區", "新界-屯門區", "新界-元朗區", "新界-葵青區", "新界-離島區"] },
- { name: "澳門", cities: ["花地瑪堂區", "聖安多尼堂區", "大堂區", "望德堂區", "風順堂區", "嘉模堂區", "聖方濟各堂區", "路氹城"]}];
2.html頁面,主要就是放置了兩個select,使用了jQuery Mobile:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <link rel="stylesheet"
- href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css" />
- <title>meetwe</title>
- </head>
- <body>
- <table width="100%" cellpadding="10">
- <tr>
- <td align="center">請選擇省份</td>
- <td align="left"><select id="selectProvince">
- <option value="-1">請選擇省份</option>
- </select></td>
- </tr>
- <tr>
- <td align="center">請選擇城市</td>
- <td align="left"><select id="selectCity">
- <option value="-1">請選擇城市</option>
- </select></td>
- </tr>
- </table>
- <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
- <script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
- <script src="../js/province-and-city-json.js"></script>
- <script src="../js/create-province-and-city.js"></script>
- </body>
- </html>
3.js程式碼實現填充下拉選單選項,並響應省一級下拉選單選中項改變事件更改市級下拉選單取值:
- $(document).on("pageinit", function() {
- // 填充省下拉選單
- var provincesOptionHtml = "";
- var provinces = [];
- for(var el in provinceAndCityArray) {
- provinces.push(provinceAndCityArray[el].name);
- provincesOptionHtml += "<option value=" + el + ">" + provinceAndCityArray[el].name + "</option>";
- }
- $("#selectProvince").append(provincesOptionHtml);
- // 填充城市下拉選單
- $("#selectProvince").bind("change", function() {
- var selectedProvince = $("#selectProvince :selected").val();
- if(selectedProvince != -1) {
- var citiesOptionHtml = "";
- var cities = [];
- cities = provinceAndCityArray[selectedProvince].cities;
- for(var elCity in cities) {
- citiesOptionHtml += "<option value=" + elCity + ">" + cities[elCity] + "</option>";
- }
- // 清空之前的城市列表
- $("#selectCity option[value!=-1]").remove();
- $("#selectCity option[value=-1]").attr("selected", true);
- $("#selectCity").append(citiesOptionHtml);
- $("#selectCity").selectmenu("refresh");
- }
- });
- });