懂得放弃,才会有新的收获!

多级关联下拉(select)菜单代码

By weisnet On 2009-05-04 At 20:14 View:604

JavaScript
  1. ///selPlace.js
  2. ////////////////////Place Database//////////////////////////
  3. // Power By gloot Copyright!!
  4. ////////////////////////////////////////////////////////////
  5. var PlaceCtl = function() {}
  6. PlaceCtl.prototype.Place = {
  7.     qz: {
  8.         Name:"泉州",
  9.         ID:"1",
  10.         Places: { //Places s
  11.             fz:{
  12.                 Name:"丰泽区",
  13.                 ID:"1"
  14.             },
  15.             lc: {
  16.                 Name:"鲤城区",
  17.                 ID:"2"
  18.             },
  19.             lj: {
  20.                 Name:"洛江区",
  21.                 ID:"3"
  22.             },
  23.             qg: {
  24.                 Name:"泉港区",
  25.                 ID:"4"
  26.             },
  27.             ha: {
  28.                 Name:"惠安县",
  29.                 ID:"5"
  30.             },
  31.             jj: {
  32.                 Name:"晋江市",
  33.                 ID:"6"
  34.             },
  35.             ss: {
  36.                 Name:"石狮市",
  37.                 ID:"7"
  38.             },
  39.             la: {
  40.                 Name:"南安市",
  41.                 ID:"8"
  42.             },
  43.             dh: {
  44.                 Name:"德化县",
  45.                 ID:"9"
  46.             },
  47.             ax: {
  48.                 Name:"安溪县",
  49.                 ID:"10"
  50.             },
  51.             yc: {
  52.                 Name:"永春",
  53.                 ID:"1"
  54.             },
  55.             kfq: {
  56.                 Name:"开发区",
  57.                 ID:"1"
  58.             }
  59.         } ////Places e
  60.     }, //泉州 end
  61.   
  62.     xm: {
  63.         Name:"厦门",
  64.         ID:"2",
  65.         Places: { //Places s
  66.             ta: {
  67.                 Name:"同安区",
  68.                 ID:"1"
  69.             },
  70.             sa: {
  71.                 Name:"翔安区",
  72.                 ID:"2"
  73.             }
  74.         }//Places e
  75.     } //厦门 e
  76. }

  

\///调用页面 TestW.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestW.aspx.cs" Inherits="TestW" %>

<!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 runat="server">
    <title>测试页面</title>
    <script type="text/javascript" src="js/selPlace.js"></script>
    <script type="text/javascript">
        var Pl = new PlaceCtl();
       
        function loadDr()
        {
            for (var items in Pl.Place)
            {
                document.getElementById('bc').options.add(new Option(Pl.Place[items].Name,Pl.Place[items].ID));
            }
        }
       
        function setSc(obj)
        {
            document.getElementById('sc').options.length = 1;
            for (var items in Pl.Place)
            {
                if (Pl.Place[items].Name == obj.options[obj.selectedIndex].text)
                {
                    for (itemsc in Pl.Place[items].Places)
                    {
                        document.getElementById('sc').options.add(new Option(Pl.Place[items].Places[itemsc].Name,Pl.Place[items].Places[itemsc].ID));
                    }
                }
            }
           
            //alert(document.body.innerHTML)
        }
    </script>
</head>
<body onload="loadDr()">
    <form id="form1" runat="server">
    <div>
        <select name="bc" id="bc" onchange="setSc(this)"><option>请选择</option></select> &nbsp; <select name="sc" id="sc"><option>请选择</option></select>
    </div>
    </form>
</body>
</html>

 

 

//如果把.js文件里的PlaceCtl.prototype.Place项,无限扩展,就可实现多级联动的下拉菜单. 给最后一个select前的每一个select添加相应的onchange, 写相应的方法.

 

并相应改变:

for (var items in Pl.Place)
            {
                if (Pl.Place[items].Name == obj.options[obj.selectedIndex].text)
                {
                    for (itemsc in Pl.Place[items].Places)
                    {
                        document.getElementById('sc').options.add(new Option(Pl.Place[items].Places[itemsc].Name,Pl.Place[items].Places[itemsc].ID));
                    }
                }
            }
           

的item项,即可实现.

·上一篇:Ajax的小方法(Version 2.1)
·下一篇:Request.Form('content1')与content1.value

for this post

 
 

Leave a Reply

赶快抢个沙发坐坐!
请先 登陆  还没帐号

Hot Posts Powered by weiseditor

  • Add to Google

Friend links