日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

二级联动,三级联动,初学者,纯javascript,不含jQuery

發布時間:2025/4/14 javascript 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 二级联动,三级联动,初学者,纯javascript,不含jQuery 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

二級聯動:

html代碼:

1 <body> 2 <select id="province" onchange="getCity(this.options.selectedIndex)"> 3 <option>請選擇</option> 4 <option>廣東</option> 5 <option>廣西</option> 6 </select>&nbsp; 7 <select id="city"> 8 <option>請選擇</option> 9 </select>&nbsp; 10 </body>

js代碼:

1 var citys=[ 2 ["廣州","佛山","深圳"], 3   ["柳州","桂林"] 4 ]; 5 function getCity(index){ 6 var city=document.getElementById('city'); 7 var showCity=citys[index-1]; 8 city.length=1; 9 for(var i=0;i<showCity.length;i++){ 10 city.options[i+1]=new Option(showCity[i]); 11 } 12 }

三級聯動:

html代碼:

1 <body> 2 <select id="province" onchange="getCity()"> 3 <option>請選擇</option> 4 <option>廣東</option> 5 <option>廣西</option> 6 </select>&nbsp; 7 <select id="city" onchange="getArea()"> 8 <option>請選擇</option> 9 </select>&nbsp; 10 <select id="area"> 11 <option>請選擇</option> 12 </select>13 </body>

js代碼:

1 var city = [ 2 ["廣州", "朝陽", "潮州", "汕頭"], 3 ["柳州", "桂林"] 4 ]; 5 var areaa = [ 6 [ 7 ["花都", "越秀", "荔灣", "天河", "海珠", "增城", "從化"], 8 ["雙塔", "龍城"], 9 ["湘橋", "潮安"], 10 ["龍湖", "濠江", "朝南", "澄海"] 11 ], 12 [ 13 ["柳江", "柳南", "柳北"], 14 ["疊彩", "秀峰", "七星", "雁山", "臨桂"] 15 ] 16 ]; 17 18 function getCity() { 19 var prov = document.getElementById("province"); 20 var ci = document.getElementById("city"); 21 var ar = document.getElementById('area'); 22 var provinceCity = city[prov.selectedIndex - 1]; 23 ci.length = 1; //為了處理數組切換時數據錯亂 24 if(prov.selectedIndex != 0) { 25 for(var i = 0; i < provinceCity.length; i++) { 26 ci[i + 1] = new Option(provinceCity[i]); 27 //以下寫法也可以 28 //ci.options[i+1]=new Option(provinceCity[i]); 29 } 30 } 31 ar.length = 1; 32 } 33 34 function getArea() { 35 var prov = document.getElementById("province"); 36 var ar = document.getElementById('area'); 37 var ci = document.getElementById("city"); 38 var provinceCityArea = areaa[prov.selectedIndex - 1][ci.selectedIndex - 1]; 39 ar.length = 1; //為了處理數組切換時數據錯亂 40 if(ci.selectedIndex != 0) { 41 for(var i = 0; i < provinceCityArea.length; i++) { 42 ar[i + 1] = new Option(provinceCityArea[i]); 43 } 44 } 45 46 }

?

轉載于:https://www.cnblogs.com/Meiwah/p/10565458.html

總結

以上是生活随笔為你收集整理的二级联动,三级联动,初学者,纯javascript,不含jQuery的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。