用javascript編寫地區表單ES6
阿新 • • 發佈:2018-06-09
onchange 淮安 ava 滄州 initial 泉州 pan 張家界 清空
1 <!DOCTYPE html> 2 <html lang="en"> 3 4 <head> 5 <meta charset="UTF-8"> 6 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 7 <meta http-equiv="X-UA-Compatible" content="ie=edge"> 8 <title>Document</title> 9</head> 10 11 <body> 12 13 <select id="province"> 14 <option value="0">北京</option> 15 <option value="1">上海</option> 16 <option value="2">天津</option> 17 <option value="3">重慶</option> 18 <option value="4">河北</option> 19<option value="5">山西</option> 20 <option value="6">內蒙古</option> 21 <option value="7">遼寧</option> 22 <option value="8">吉林</option> 23 <option value="9">黑龍江</option> 24 <option value="10">江蘇</option> 25<option value="11">浙江</option> 26 <option value="12">安徽</option> 27 <option value="13">福建</option> 28 <option value="14">江西</option> 29 <option value="15">山東</option> 30 <option value="16">河南</option> 31 <option value="17">湖北</option> 32 <option value="18">湖南</option> 33 <option value="19">廣東</option> 34 <option value="20">廣西</option> 35 <option value="21">海南</option> 36 <option value="22">四川</option> 37 <option value="23">貴州</option> 38 <option value="24">雲南</option> 39 <option value="25">西藏</option> 40 <option value="26">陜西</option> 41 <option value="27">甘肅</option> 42 <option value="28">寧夏</option> 43 <option value="29">青海</option> 44 <option value="30">新疆</option> 45 <option value="31">香港</option> 46 <option value="32">澳門</option> 47 <option value="33">臺灣</option> 48 </select> 49 50 <select name="" id="city"> 51 52 </select> 53 54 <script> 55 let cities = new Array(); 56 cities[0] = "東城,西城,崇文,宣武,朝陽,豐臺,石景山,海澱,門頭溝,房山,通州,順義,昌平,大興,平谷,懷柔,密雲,延慶"; 57 cities[1] = "黃浦,盧灣,徐匯,長寧,靜安,普陀,閘北,虹口,楊浦,閔行,寶山,嘉定,浦東,金山,松江,青浦,南匯,奉賢,崇明"; 58 cities[2] = "和平,東麗,河東,西青,河西,津南,南開,北辰,河北,武清,紅撟,塘沽,漢沽,大港,寧河,靜海,寶坻,薊縣"; 59 cities[3] = "萬州,涪陵,渝中,大渡口,江北,沙坪壩,九龍坡,南岸,北碚,萬盛,雙撟,渝北,巴南,黔江,長壽,綦江,潼南,銅梁,大足,榮昌,壁山,梁平,城口,豐都,墊江,武隆,忠縣,開縣,雲陽,奉節,巫山,巫溪,石柱,秀山,酉陽,彭水,江津,合川,永川,南川"; 60 cities[4] = "石家莊,邯鄲,邢臺,保定,張家口,承德,廊坊,唐山,秦皇島,滄州,衡水"; 61 cities[5] = "太原,大同,陽泉,長治,晉城,朔州,呂梁,忻州,晉中,臨汾,運城"; 62 cities[6] = "呼和浩特,包頭,烏海,赤峰,呼倫貝爾盟,阿拉善盟,哲裏木盟,興安盟,烏蘭察布盟,錫林郭勒盟,巴彥淖爾盟,伊克昭盟"; 63 cities[7] = "沈陽,大連,鞍山,撫順,本溪,丹東,錦州,營口,阜新,遼陽,盤錦,鐵嶺,朝陽,葫蘆島"; 64 cities[8] = "長春,吉林,四平,遼源,通化,白山,松原,白城,延邊"; 65 cities[9] = "哈爾濱,齊齊哈爾,牡丹江,佳木斯,大慶,綏化,鶴崗,雞西,黑河,雙鴨山,伊春,七臺河,大興安嶺"; 66 cities[10] = "南京,鎮江,蘇州,南通,揚州,鹽城,徐州,連雲港,常州,無錫,宿遷,泰州,淮安"; 67 cities[11] = "杭州,寧波,溫州,嘉興,湖州,紹興,金華,衢州,舟山,臺州,麗水"; 68 cities[12] = "合肥,蕪湖,蚌埠,馬鞍山,淮北,銅陵,安慶,黃山,滁州,宿州,池州,淮南,巢湖,阜陽,六安,宣城,亳州"; 69 cities[13] = "福州,廈門,莆田,三明,泉州,漳州,南平,龍巖,寧德"; 70 cities[14] = "南昌市,景德鎮,九江,鷹潭,萍鄉,新餘,贛州,吉安,宜春,撫州,上饒"; 71 cities[15] = "濟南,青島,淄博,棗莊,東營,煙臺,濰坊,濟寧,泰安,威海,日照,萊蕪,臨沂,德州,聊城,濱州,菏澤"; 72 cities[16] = "鄭州,開封,洛陽,平頂山,安陽,鶴壁,新鄉,焦作,濮陽,許昌,漯河,三門峽,南陽,商丘,信陽,周口,駐馬店,濟源"; 73 cities[17] = "武漢,宜昌,荊州,襄樊,黃石,荊門,黃岡,十堰,恩施,潛江,天門,仙桃,隨州,鹹寧,孝感,鄂州"; 74 cities[18] = "長沙,常德,株洲,湘潭,衡陽,嶽陽,邵陽,益陽,婁底,懷化,郴州,永州,湘西,張家界"; 75 cities[19] = "廣州,深圳,珠海,汕頭,東莞,中山,佛山,韶關,江門,湛江,茂名,肇慶,惠州,梅州,汕尾,河源,陽江,清遠,潮州,揭陽,雲浮"; 76 cities[20] = "南寧,柳州,桂林,梧州,北海,防城港,欽州,貴港,玉林,南寧地區,柳州地區,賀州,百色,河池"; 77 cities[21] = "海口,三亞"; 78 cities[22] = "成都,綿陽,德陽,自貢,攀枝花,廣元,內江,樂山,南充,宜賓,廣安,達川,雅安,眉山,甘孜,涼山,瀘州"; 79 cities[23] = "貴陽,六盤水,遵義,安順,銅仁,黔西南,畢節,黔東南,黔南"; 80 cities[24] = "昆明,大理,曲靖,玉溪,昭通,楚雄,紅河,文山,思茅,西雙版納,保山,德宏,麗江,怒江,迪慶,臨滄"; 81 cities[25] = "拉薩,日喀則,山南,林芝,昌都,阿裏,那曲"; 82 cities[26] = "西安,寶雞,鹹陽,銅川,渭南,延安,榆林,漢中,安康,商洛"; 83 cities[27] = "蘭州,嘉峪關,金昌,白銀,天水,酒泉,張掖,武威,定西,隴南,平涼,慶陽,臨夏,甘南"; 84 cities[28] = "銀川,石嘴山,吳忠,固原"; 85 cities[29] = "西寧,海東,海南,海北,黃南,玉樹,果洛,海西"; 86 cities[30] = "烏魯木齊,石河子,克拉瑪依,伊犁,巴音郭勒,昌吉,克孜勒蘇柯爾克孜,博 爾塔拉,吐魯番,哈密,喀什,和田,阿克蘇"; 87 cities[31] = "香港"; 88 cities[32] = "澳門"; 89 cities[33] = "臺北,高雄,臺中,臺南,屏東,南投,雲林,新竹,彰化,苗栗,嘉義,花蓮,桃園,宜蘭,基隆,臺東,金門,馬祖,澎湖"; 90 let city = document.getElementById(‘city‘); 91 let province = document.getElementById(‘province‘); 92 function cit() { 93 let i = province.value; 94 let ne = cities[i].split(‘,‘); 95 for (let j = 0; j < ne.length; j++) { 96 let o = new Option(`${ne[j]}`) 97 city.options.add(o); 98 } 99 } 100 cit(); 101 102 province.onchange = function () {//獲取province中選中的value值 103 city.options.length = 0;//清空選中的值 104 let i = province.value;//將選中的value值保存在i中 105 let ne = cities[i].split(‘,‘);//將字符串變為數組 106 for (let j = 0; j < ne.length; j++) {//將cities數組便利出來 107 let o = new Option(`${ne[j]}`)//創建option 108 city.options.add(o);//添加option 109 } 110 } 111 112 113 114 115 116 117 118 119 120 </script> 121 122 123 </body> 124 125 </html>
用javascript編寫地區表單ES6