三級聯動是表單裡很常見的需求:選了省份,城市清單跟著更新;選了城市,縣市清單再跟著更新。本文用純 JavaScript 實作,不依賴任何函式庫,資料以巢狀陣列組織,替換成真實資料源時只要改最上面那一段即可。
//省市縣數據格式 var province_city_county_data=[ { province:"四川", city:[ { cityName:"成都", county:["成都市","崇州市","金堂縣"] }, { cityName:"南充", county:["儀隴縣","南部縣","營山縣"] } ] }, { province:"北京", city:[ { cityName:"北京市", county:["東城區","朝陽區"] } ] }, { province:"安徽", city:[ { cityName:"安慶", county:["安慶市","懷寧縣","潛山縣"] }, { cityName:"蚌埠", county:["蚌埠市","固鎮縣","懷遠縣"] } ] } ]
var opt0 = ["省份","地級市","市、縣級市、縣"]; var selectID_arr2=["provinceid","cityid","countyid"]; var province_index; window.onload = function(){ //初始化下拉框 function init_select(){ init_title(); init_province(); bind_province(); } //初始化提示標題 function init_title(){ for(var k = 0;k<selectID_arr2.length;k++){ var select_obj= document.getElementById(selectID_arr2[k]); select_obj.options[0]=new Option(opt0[k],opt0[k]); } } //初始化省份 function init_province(){ var province_select_obj = document.getElementById(selectID_arr2[0]); var province_len = province_city_county_data.length; for(var i = 0;i<province_len;i++){ province_select_obj.options[i+1] = new Option(province_city_county_data[i].province,province_city_county_data[i].province); } } //給省份綁定onchange事件 function bind_province(){ var province_select_obj = document.getElementById(selectID_arr2[0]); province_select_obj.onchange=function(){ var opt_index =province_select_obj.selectedIndex; if(opt_index!=0){ province_index = opt_index-1; //每個省份的序號比 option 的下標要小1 init_city(province_index); bind_city(); init_county(province_index,0); }else{ clear_city(); clear_county(); } } } //選擇一個省份才能初始化地級市 function init_city(index){ clear_city(); var city_len = province_city_county_data[index].city.length; for(var i = 0;i<city_len;i++){ document.getElementById(selectID_arr2[1]).options[i+1] = new Option(province_city_county_data[index].city[i].cityName,province_city_county_data[index].city[i].cityName); } document.getElementById(selectID_arr2[1]).options[1].selected = true; } //清除地級市信息 function clear_city(){ var city_select_obj = document.getElementById(selectID_arr2[1]); city_select_obj.options.length=0; //每次選中一個新的省份 都重新刪除地級市的信息 init_title(); //重新初始化標題 } //給地級市綁定onchange事件 function bind_city(){ var city_select_obj = document.getElementById(selectID_arr2[1]); city_select_obj.onchange=function(){ var opt_index =city_select_obj.selectedIndex; if(opt_index!=0){ init_county(province_index,opt_index-1); }else{ clear_county(); } } } //選擇一個地級市後才能初始化縣 function init_county(index,city_index){ clear_county(); var county_len = province_city_county_data[index].city[city_index].county.length; for(var i = 0;i<county_len;i++){ document.getElementById(selectID_arr2[2]).options[i+1] = new Option(province_city_county_data[index].city[city_index].county[i],province_city_county_data[index].city[city_index].county[i]); } document.getElementById(selectID_arr2[2]).options[1].selected = true; } //清除縣城信息 function clear_county(){ var county_select_obj = document.getElementById(selectID_arr2[2]); county_select_obj.options.length=0; //每次選中一個新的地級市 都重新刪除縣的信息 init_title(); //重新初始化標題 } init_select() }
<select id="provinceid"></select> <select id="cityid"></select> <select id="countyid"></select>
索引要記得偏移量。每個下拉選單的第 0 個 option 是「請選擇」提示,真正的資料從索引 1 開始,因此 selectedIndex 減 1 才是資料陣列的索引。這是全篇最容易出錯的地方。
切換前先清空下游。clear_city 與 clear_county 用 options.length = 0 一次清掉所有選項,再呼叫 init_title 補回提示文字。少了這一步,切換省份後會殘留上一次的選項。
清空順序由外而內。換省份時依序重建城市與縣市;反之若使用者把選單退回提示值,就必須把下游一并清空,避免出現「城市還在、縣市卻已對不上」的錯亂狀態。
若要接真實資料,建議把這份程式再往前推進一步:將資料抽成 JSON 檔動態載入,並在初始化時只呼叫一次 bind_province(目前放在 init_city 之後重複綁定,雖然以 onchange 赋值形式不會累積事件,但邏輯上移到啟動階段會更清楚)。
更多 JavaScript 相關內容可參考本站專題:《JavaScript 中 json 操作技巧總結》、《JavaScript 動畫特效與技巧匯總》、《JavaScript 擴展技巧總結》、《JavaScript 運動效果與技巧匯總》、《JavaScript 數學運算用法總結》及《JavaScript 面向對象入門教程》。