您好,登錄后才能下訂單哦!
三級聯動考察的應該是對于數據的處理,只要清楚其中的關系,再多幾級都是一樣的:
html部分:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>3級聯動菜單</title> <script> var region = { 廣東: { "廣州":["廣州1","廣州2","廣州3"], "珠海":["珠海1","珠海2","珠海3"], "佛山":["佛山1"] }, 湖南: { "常德":["石門","桃源","臨澧","漢壽"], "益陽":["益陽1","益陽2","益陽3"] } } </script> </head> <body> <select id="province" onchange="change(this.value);"> <option>請選擇省份</option> </select> <select id="city" onchange="countyChange(this.value);"> <option>請選擇地市</option> </select> <select id="county"> <option>請選擇縣城</option> </select> <script src='test.js'></script> </body> </html>
js部分:
var province = document.querySelector("#province"); var city = document.querySelector("#city"); var county = document.querySelector("#county"); //二級聯動不用定義你選的省份,直接用省份(key)來決定下面的市(value)值 var provinceName = null; for (ele in region){ var op = new Option(ele , ele , false , false); //new Option("文本","值",true,true).后面兩個true分別表示默認被選中和有效 //console.log(op); province.options[province.length] = op; } var change = function(src){ city.innerHTML = ""; if(src === '請選擇省份'){ var op = new Option('請選擇地市' , '請選擇地市' , false , false); city.options[0] = op; }else{ for (index in region[src]){ //console.log(index); var op = new Option(index , index , false , false); city.options[city.length] = op; } } //記住選擇省份的值 provinceName=src; countyChange(city.value) } var countyChange = function(src2){ county.innerHTML = ""; if(src2 === '請選擇地市'){ var op = new Option('請選擇縣城','請選擇縣城', false , false); county.options[0] = op; }else{ for (index in region[provinceName][src2]){ //console.log(index); var op = new Option(region[provinceName][src2][index] , region[provinceName][src2][index] , false , false); county.options[county.length] = op; } } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。