DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> Javascript實現動態菜單添加的實例代碼
Javascript實現動態菜單添加的實例代碼
編輯:關於JavaScript     
在注冊信息的時候,常常需要通過下拉菜單讓用戶選擇,而且希望用戶在第一個下拉框做的選擇,影響第二個下拉框的內容。有時候,如果第一個下拉框不作出選擇,第二個下拉框根本不會頁面上顯示,為了給用戶呈現一個更清晰的頁面。  

先來看看效果:

Html源碼:

復制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>動態改變菜單</title> 
<script type="text/javascript" src="jquery.js"></script> 
<script type="text/javascript" src="SelectMenu.js"></script> 
</head> 
<body> 
<form action="#"> 
<br/> 
<br/> 
<br/> 
    <div class="Address"> 
        <span class="Province">Province: 
            <select> 
                <option value="" selected="selected">Please Choose Province</option> 
                <option value="HeBei">HeBei</option> 
                <option value="ShanDong">ShanDong</option> 
            </select>  
        </span> 
        <span class="City" style="display:none">City: 
            <select> 
            </select> 
        </span> 
        <span class="Area" style="display:none">Area: 
            <select> 
            </select> 
        </span> 
        <br/> 
         <br/> 
        <span class="AddressSelect" style="display:none"> 
        </span> 
    </div> 
</form> 
</body> 
</html> 


Javascript源碼

復制代碼 代碼如下:
$(document).ready(function () { 
    //找到三個下拉框  
    var ProvinceSelect = $(".Province").children("select"); 
    var CitySelect = $(".City").children("select"); 
    var AreaSelect = $(".Area").children("select"); 
    var AddressSelect=$(".AddressSelect"); 
    //給第二個下拉框注冊事件  
    ProvinceSelect.change(function () { 
        //1、獲取當前下拉框的值  
         var ProvinceValue = $(this).val(); 
        //1.1只要第一個下拉框內容有變化,第三個下拉框就要隱藏起來  
        AreaSelect.parent().hide(); 
        AddressSelect.hide(); 
        AddressSelect.html(""); 
        //2、如果值不為空,則顯示城市下拉框  
         if (ProvinceValue != "") { 
                   CitySelect.html(""); 
                   $("<option value=''>Please Choose City</option>").appendTo(CitySelect); 
                    switch(ProvinceValue) 
                       { 
                        //實際項目中,這個城市數組肯定是在服務器獲取的,這裡為了簡便,我就直接自定義了一個數組  
                        //如果追求完美,這裡還可以加一道緩存,防止重復獲取  
                       case "HeBei": 
                            var CityOfHeBei=["ShiJiaZhuang","CangZhou","LangFang"];  
                            for(var i=0;i<CityOfHeBei.length;i++){ 
                                $("<option value='"+CityOfHeBei[i]+"'>"+CityOfHeBei[i]+"</option>").appendTo(CitySelect); 
                            } 
                             break; 
                       case "ShanDong": 
                            var CityOfShanDon=["JiNan","DeZhou","QingDao"];      
                            for(var i=0;i<CityOfShanDon.length;i++){ 
                                $("<option value='"+CityOfShanDon[i]+"'>"+CityOfShanDon[i]+"</option>").appendTo(CitySelect); 
                            } 
                            break; 
                       } 
                 CitySelect.parent().show();    
        } else { 
            CitySelect.parent().hide(); 
        } 
    }); 
    //給第二個下拉框注冊事件  
     CitySelect.change(function () { 
            var CityValue = $(this).val();       
            AddressSelect.hide(); 
            AreaSelect.parent().hide();  
            AddressSelect.html(""); 
             if (CityValue != "") { 
                   AreaSelect.html(""); 
                   $("<option value=''>Please Choose Area</option>").appendTo(AreaSelect); 
                    switch(CityValue) 
                       { 
                       //實際項目中,這個區數組肯定是在服務器獲取的,這裡為了簡便,我就直接自定義了一個數組  
                       //如果追求完美,這裡還可以加一道緩存,防止重復獲取  
                       case "ShiJiaZhuang": 
                            var AreaOfCity=["GaoXinQu","KaiFaQu","XinHuaQu"];    
                            for(var i=0;i<AreaOfCity.length;i++){ 
                                $("<option value='"+AreaOfCity[i]+"'>"+AreaOfCity[i]+"</option>").appendTo(AreaSelect); 
                            } 
                             break; 
                       case "CangZhou": 
                            var AreaOfCity=["XinHuaQu","YunHeQu"];       
                            for(var i=0;i<AreaOfCity.length;i++){ 
                                $("<option value='"+AreaOfCity[i]+"'>"+AreaOfCity[i]+"</option>").appendTo(AreaSelect); 
                            } 
                            break; 
                       case "LangFang": 
                            var AreaOfCity=["AnCiQu","GuangYangQu"]; 
                            for(var i=0;i<AreaOfCity.length;i++){ 
                                $("<option value='"+AreaOfCity[i]+"'>"+AreaOfCity[i]+"</option>").appendTo(AreaSelect); 
                            } 
                            break; 
                       case "QingDao": 
                            var AreaOfCity=["GaoXinQu","KaiFaQu","XinHuaQu"];    
                            for(var i=0;i<AreaOfCity.length;i++){ 
                                $("<option value='"+AreaOfCity[i]+"'>"+AreaOfCity[i]+"</option>").appendTo(AreaSelect); 
                            } 
                             break; 
                       case "DeZhou": 
                            var AreaOfCity=["XinHuaQu","YunHeQu"];       
                            for(var i=0;i<AreaOfCity.length;i++){ 
                                $("<option value='"+AreaOfCity[i]+"'>"+AreaOfCity[i]+"</option>").appendTo(AreaSelect); 
                            } 
                            break; 
                       case "JiNan": 
                            var AreaOfCity=["AnCiQu","GuangYangQu"]; 
                            for(var i=0;i<AreaOfCity.length;i++){ 
                                $("<option value='"+AreaOfCity[i]+"'>"+AreaOfCity[i]+"</option>").appendTo(AreaSelect); 
                            } 
                            break; 
                       } 
                     AreaSelect.parent().show();  
             } else { 
                     AreaSelect.parent().hide(); 
            } 
    }); 
    AreaSelect.change(function(){ 
            var AreaValue=$(this).val(); 
            AddressSelect.html(""); 
            if (AreaValue!=""){ 
                $("<span>The Address Is --Province: "+ProvinceSelect.val()+"  City: "+CitySelect.val()+"  Area: "+AreaSelect.val()+"</span>").appendTo(AddressSelect); 
            AddressSelect.show(); 
            //alert("The Address Is  Province: "+ProvinceSelect.val()+"  City: "+CitySelect.val()+"  Area: "+AreaSelect.val());  
                }                   
     }) 
}); 


這裡還引用了Jquery,貌似實現這個效果,用不用都無所謂,最近為了熟悉Jquery的用法,所以就加上了。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved