利用JS實現一個簡單的二級聯動菜單

來源:互聯網
上載者:User

標籤:htm   dex   朝陽區   標記   深圳市   ++   網頁載入   指定   .text   

前幾天在看js的相關內容,所以就簡單寫了一個二級聯動菜單。分享一下。

 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4     <meta charset="UTF-8"> 5     <title>JS實現二級聯動菜單</title> 6 </head> 7 <body> 8     <form name="form1" method="post" action=""> 9         省份:<select name="province" id="province" onchange="changeSelect(this.selectedIndex)"></select>10         地區:<select name="city" id="city"></select>11 12     </form>13 </body>14 </html>15 <script type="text/javascript">16     var arr_province = ["請選擇省/城市","北京市","上海市","天津市","重慶市","深圳市","廣東省"];17     var arr_city = [18                     ["請選擇城市/地區"],19                     ["東城區","西城區","朝陽區","宣武區","昌平區","大興區","丰台區","海澱區"],20                     [‘寶山區‘,‘長寧區‘,‘豐賢區‘, ‘虹口區‘,‘黃浦區‘,‘青浦區‘,‘南匯區‘,‘徐匯區‘,‘盧灣區‘],21                     [‘和平區‘, ‘河西區‘, ‘南開區‘, ‘河北區‘, ‘河東區‘, ‘紅橋區‘, ‘塘古區‘, ‘開發區‘],22                     [‘俞中區‘, ‘南岸區‘, ‘江北區‘, ‘沙坪壩區‘, ‘九龍坡區‘, ‘渝北區‘, ‘大渡口區‘, ‘北碚區‘],23                     [‘福田區‘, ‘羅湖區‘, ‘鹽田區‘, ‘寶安區‘, ‘龍崗區‘, ‘南山區‘, ‘深圳周邊‘],24                     [‘廣州市‘,‘惠州市‘,‘汕頭市‘,‘珠海市‘,‘佛山市‘,‘中山市‘,‘東莞市‘]25                 ];26     //網頁載入完成,初始化菜單27     window.onload = init;//傳入函數地址28     function init(){29         //首先擷取對象30         var province = document.form1.province;31         var city = document.form1.city;32 33         //指定省份中<option>標記的個數34         province.length = arr_province.length;35 36         //迴圈將數組中的資料寫入<option>標記中37         for(var i=0;i<arr_province.length;i++){38             province.options[i].text = arr_province[i];39             province.options[i].value = arr_province[i];40         }41 42         //修改省份列表的預設選擇項43         var index = 0;44         province.selectedIndex = index;45 46         //指定城市中<option>標記的個數47         city.length = arr_city[index].length;48 49         //迴圈將數組中的資料寫入<option>標記中50         for (var j = 0; j<arr_city[index].length;j++) {51             city.options[j].text = arr_city[index][j];52             city.options[j].value = arr_city[index][j];53         }54 55     }56 57     function  changeSelect(index){58         //選擇對象59         var city = document.form1.city;60         //修改省份列表的選擇項61         province.selectedIndex = index;62 63         //指定城市中<option>標記的個數64         city.length = arr_city[index].length;65 66         //迴圈將數組中的資料寫入<option>標記中67         for (var j = 0; j<arr_city[index].length;j++) {68             city.options[j].text = arr_city[index][j];69             city.options[j].value = arr_city[index][j];70         }71     }72 73 </script>

 

利用JS實現一個簡單的二級聯動菜單

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.