Javascript實現動態菜單添加

在註冊信息的時候,常常需要通過下拉菜單讓用戶選擇,而且希望用戶在第一個下拉框做的選擇,影響第二個下拉框的內容。有時候,如果第一個下拉框不作出選擇,第二個下拉框根本不會頁面上顯示,為瞭給用戶呈現一個更清晰的頁面。

 
先來看看效果:

 

 

Html源碼:
[html]
1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
2. <html xmlns="http://www.w3.org/1999/xhtml"> 
3. <head> 
4.  
5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
6. <title>動態改變菜單</title> 
7. <script type="text/javascript" src="jquery.js"></script> 
8. <script type="text/javascript" src="SelectMenu.js"></script> 
9.  
10. </head> 
11.  
12. <body> 
13. <form action="#"> 
14. <br/> 
15. <br/> 
16. <br/> 
17.  
18.     <p class="Address"> 
19.         
20.         <span class="Province">Province: 
21.             <select> 
22.                 <option value="" selected="selected">Please Choose Province</option> 
23.                 <option value="HeBei">HeBei</option> 
24.                 <option value="ShanDong">ShanDong</option> 
25.             </select>  
26.         </span> 
27.     
28.         <span class="City" style="display:none">City: 
29.             <select> 
30.             </select> 
31.         </span> 
32.         <span class="Area" style="display:none">Area: 
33.             <select> 
34.             </select> 
35.         </span> 
36.         <br/> 
37.          <br/> 
38.         <span class="AddressSelect" style="display:none"> 
39.         </span> 
40.     </p> 
41.  
42. </form> 
43. </body> 
44. </html> 
 
Javascript源碼
[javascript]
1. $(document).ready(function () { 
2.      
3.     //找到三個下拉框  
4.     var ProvinceSelect = $(".Province").children("select"); 
5.     var CitySelect = $(".City").children("select"); 
6.     var AreaSelect = $(".Area").children("select"); 
7.     var AddressSelect=$(".AddressSelect"); 
8.      
9.     //給第二個下拉框註冊事件  
10.     ProvinceSelect.change(function () { 
11.                                  
12.         //1、獲取當前下拉框的值  
13.          var ProvinceValue = $(this).val(); 
14.         //1.1隻要第一個下拉框內容有變化,第三個下拉框就要隱藏起來  
15.         AreaSelect.parent().hide(); 
16.         AddressSelect.hide(); 
17.         AddressSelect.html(""); 
18.         //2、如果值不為空,則顯示城市下拉框  
19.          if (ProvinceValue != "") { 
20.                    CitySelect.html(""); 
21.                    $("<option value=''>Please Choose City</option>").appendTo(CitySelect); 
22.                     switch(ProvinceValue) 
23.                        { 
24.                         //實際項目中,這個城市數組肯定是在服務器獲取的,這裡為瞭簡便,我就直接自定義瞭一個數組  
25.                         //如果追求完美,這裡還可以加一道緩存,防止重復獲取  
26.                        case "HeBei": 
27.                             var CityOfHeBei=["ShiJiaZhuang","CangZhou","LangFang"];  
28.                             for(var i=0;i<CityOfHeBei.length;i++){ 
29.                                 $("<option value='"+CityOfHeBei[i]+"'>"+CityOfHeBei[i]+"</option>").appendTo(CitySelect); 
30.                             } 
31.                              break; 
32.                        case "ShanDong": 
33.                             var CityOfShanDon=["JiNan","DeZhou","QingDao"];      
34.                             for(var i=0;i<CityOfShanDon.length;i++){ 
35.                                 $("<option value='"+CityOfShanDon[i]+"'>"+CityOfShanDon[i]+"</option>").appendTo(CitySelect); 
36.                             } 
37.                             break; 
38.                      
39.                        } 
40.                  CitySelect.parent().show();    
41.         } else { 
42.             CitySelect.parent().hide(); 
43.         } 
44.  
45.     }); 
46.  
47.   
48.  
49.     //給第二個下拉框註冊事件  
50.      CitySelect.change(function () { 
51.              
52.             var CityValue = $(this).val();       
53.             AddressSelect.hide(); 
54.             AreaSelect.parent().hide();  
55.             AddressSelect.html(""); 
56.              if (CityValue != "") { 
57.                    AreaSelect.html(""); 
58.                    $("<option value=''>Please Choose Area</option>").appendTo(AreaSelect); 
59.                     switch(CityValue) 
60.                        { 
61.                        //實際項目中,這個區數組肯定是在服務器獲取的,這裡為瞭簡便,我就直接自定義瞭一個數組  
62.                        //如果追求完美,這裡還可以加一道緩存,防止重復獲取  
63.                        case "ShiJiaZhuang": 
64.                             var AreaOfCity=["GaoXinQu","KaiFaQu","XinHuaQu"];    
65.                             for(var i=0;i<AreaOfCity.length;i++){ 
66.                                 $("<option value='"+AreaOfCity[i]+"'>"+AreaOfCity[i]+"</option>").appendTo(AreaSelect); 
67.                             } 
68.                              break; 
69.                        case "CangZhou": 
70.                             var AreaOfCity=["XinHuaQu","YunHeQu"];       
71.                             for(var i=0;i<AreaOfCity.length;i++){ 
72.                                 $("<option value='"+AreaOfCity[i]+"'>"+AreaOfCity[i]+"</option>").appendTo(AreaSelect); 
73.                             } 
74.                             break; 
75.                        case "LangFang": 
76.                             var AreaOfCity=["AnCiQu","GuangYangQu"]; 
77.                             for(var i=0;i<AreaOfCity.length;i++){ 
78.                                 $("<option value='"+AreaOfCity[i]+"'>"+AreaOfCity[i]+"</option>").appendTo(AreaSelect); 
79.                             } 
80.                             break; 
81.                        case "QingDao": 
82.                             var AreaOfCity=["GaoXinQu","KaiFaQu","XinHuaQu"];    
83.                             for(var i=0;i<AreaOfCity.length;i++){ 
84.                                 $("<option value='"+AreaOfCity[i]+"'>"+AreaOfCity[i]+"</option>").appendTo(AreaSelect); 
85.                             } 
86.                              break; 
87.                        case "DeZhou": 
88.                             var AreaOfCity=["XinHuaQu","YunHeQu"];       
89.                             for(var i=0;i<AreaOfCity.length;i++){ 
90.                                 $("<option value='"+AreaOfCity[i]+"'>"+AreaOfCity[i]+"</option>").appendTo(AreaSelect); 
91.                             } 
92.                             break; 
93.                        case "JiNan": 
94.                             var AreaOfCity=["AnCiQu","GuangYangQu"]; 
95.                             for(var i=0;i<AreaOfCity.length;i++){ 
96.                                 $("<option value='"+AreaOfCity[i]+"'>"+AreaOfCity[i]+"</option>").appendTo(AreaSelect); 
97.                             } 
98.                             break; 
99.                        } 
100.                      AreaSelect.parent().show();  
101.                  
102.                       
103.              } else { 
104.                      AreaSelect.parent().hide(); 
105.             } 
106.  
107.     }); 
108.  
109.     AreaSelect.change(function(){ 
110.             var AreaValue=$(this).val(); 
111.             AddressSelect.html(""); 
112.             if (AreaValue!=""){ 
113.                 $("<span>The Address Is –Province: "+ProvinceSelect.val()+"  City: "+CitySelect.val()+"  Area: "+AreaSelect.val()+"</span>").appendTo(AddressSelect); 
114.             AddressSelect.show(); 
115.             //alert("The Address Is  Province: "+ProvinceSelect.val()+"  City: "+CitySelect.val()+"  Area: "+AreaSelect.val());  
116.                 }                   
117.      }) 
118.  
119. }); 
 
這裡還引用瞭Jquery,貌似實現這個效果,用不用都無所謂,最近為瞭熟悉Jquery的用法,所以就加上瞭。

 摘自 賈琳的專欄
 

發佈留言