Ajax實現聯動效果

這類東西很多人都寫過,但我是第一次寫,支持一下菜鳥的原創吧~
第一級是寫死的(因爲省不會變的),以下三級都是現查的數據庫,由於沒有采用DIV格式,所以有些ID很長~
js代碼:

1function ProvinceChange(obj1,obj2,obj3)
    2 {
    3        if(typeof(ActiveXObject) != "undefined")
    4        {
    5                if(obj1.value=="")
    6                {
    7                        obj2.length=0;
    8                        obj3.length=0;
    9                        form1.Wizard1_SchoolAddForm1_District.length=0;
10                }
11                else
12                {
13                        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
14                        var item = obj1.options[obj1.selectedIndex].value;
15                        var ReqStr = '<?xml version="1.0"?>';                    
16                        xmlHttp.open("GET","SettleForm.aspx?province="+item);
17                        xmlHttp.send(ReqStr);
18                        xmlHttp.onreadystatechange = HandleResult;
19                }
20        }
21        function HandleResult()
22        {
23             if(4 == xmlHttp.readyState || "complete" == xmlHttp.readyState)
24             {
25                        obj2.length=0;
26                        obj3.length=0;
27                        form1.Wizard1_SchoolAddForm1_District.length=0;
28                        obj2.options[obj2.length]=new Option("");
29                        obj3.options[obj3.length]=new Option("");
30                        var e=xmlHttp.responseText;
31                        alert(e);
32                        if(e!="未添加")
33                        {
34                                var a=e.split("*");
35                                var arr1=a[0].toString().split(",");
36                                var arr2=a[1].toString().split(",");
37                                for(var i=0;i<arr1.length;i++)
38                                {                                                
39                                        obj2.options[obj2.length]=new Option(arr1[i]);
40                                }
41                                for(var j=0;j<arr2.length;j++)
42                                {                                                
43                                        obj3.options[obj3.length]=new Option(arr2[j]);
44                                }
45                        }
46             }
47        }
48 }
49    
50 function CityChange(obj1,obj2,obj3)
51 {
52        if(typeof(ActiveXObject) != "undefined")
53        {
54                if(obj1.selectedIndex==0)
55                {
56                         obj2.length=0;
57                         obj3.length=0;
58                }
59                else
60                {
61                        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
62                        var ReqStr = '<?xml version="1.0"?>';    
63                        var item = obj1.options[obj1.selectedIndex].text;                
64                        xmlHttp.open("GET","SettleForm.aspx?city="+item);
65                        xmlHttp.send(ReqStr);
66                        xmlHttp.onreadystatechange = HandleResult;
67                }
68        }
69        function HandleResult()
70        {
71             if(4 == xmlHttp.readyState || "complete" == xmlHttp.readyState)
72             {
73                        obj2.length=0;
74                        obj2.options[obj2.length]=new Option("");
75                        obj3.length=0;
76                        obj3.options[obj3.length]=new Option("");
77                        var e=xmlHttp.responseText;
78                        if(e!="未添加")
79                        {
80                                var a=e.split("*");
81                                var arr1=a[0].toString().split(",");
82                                var arr2=a[1].toString().split(",");
83                                for(var i=0;i<arr1.length;i++)
84                                {                                                
85                                        obj2.options[obj2.length]=new Option(arr1[i]);
86                                }
87                                for(var j=0;j<arr2.length;j++)
88                                {                                                
89                                        obj3.options[obj3.length]=new Option(arr2[j]);
90                                }
91                        }    
92             }
93        }
94    
95 }
96    
97 function DistrictChange(obj1,obj2)
98 {
99        if(typeof(ActiveXObject) != "undefined")
100        {
101                if(obj1.selectedIndex==0)
102                {
103                         obj2.length=0;
104                }
105                else
106                {
107                        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
108                        var item = obj1.options[obj1.selectedIndex].text;
109                        var ReqStr = '<?xml version="1.0"?>';                    
110                        xmlHttp.open("GET","SettleForm.aspx?district="+item);
111                        xmlHttp.send(ReqStr);
112                        xmlHttp.onreadystatechange = HandleResult;
113                }
114        }
115        function HandleResult()
116        {
117             if(4 == xmlHttp.readyState || "complete" == xmlHttp.readyState)
118             {
119                        obj2.length=0;
120                        obj2.options[obj2.length]=new Option("");
121                        if(e!="未添加")
122                        {
123                                var e=xmlHttp.responseText;
124                                var arr=e.split(",")
125                                for(var i=0;i<arr.length;i++)
126                                {                                                
127                                        obj2.options[obj2.length]=new Option(arr[i]);
128                                }
129                        }
130             }
131        }
132 }
133
 
中間頁代碼:
public partial class BusinessmanManage_SettleForm : System.Web.UI.Page
19{
20        protected ShoppingChain.BLL.Product p = new ShoppingChain.BLL.Product();
21        protected ShoppingChain.BLL.Register r = new ShoppingChain.BLL.Register();
22        protected void Page_Load(object sender, EventArgs e)
23        {
24                        if (Request.QueryString["province"] != null)
25                {
26                        IList<SchoolInfo> list1 = r.GetCityByProvince(Request.QueryString["province"]);
27                        IList<SchoolInfo> list2 = r.GetSchoolByProvince(Request.QueryString["province"]);
28                        if (list1.Count == 0)
29                        {
30                                Response.Write("未添加");
31                        }
32                        else
33                        {
34                                //string cityshort = "";
35                                string cityname = "";
36                                string school = "";
37                                foreach (SchoolInfo info in list1)
38                                {
39                                        cityname = cityname + info.City + ",";
40                                }
41                                //string str = cityshort.Remove(cityshort.Length - 1);
42                                string strcname = cityname.Remove(cityname.Length - 1);
43                                foreach (SchoolInfo info in list2)
44                                {
45                                        school = school + info.School + ",";
46                                }
47                                string strschool = school.Remove(school.Length - 1);
48                                string s = strcname + "*" + strschool;
49                                Response.Write(s);
50                        }
51                }
52                if (Request.QueryString["city"] != null)
53                {
54                        IList<SchoolInfo> list1 = r.GetDistrictByCity(Request.QueryString["city"]);
55                        IList<SchoolInfo> list2 = r.GetSchoolByCity(Request.QueryString["city"]);
56                        string district = "";
57                        string school = "";
58                        if (list1.Count == 0)
59                        {
60                                Response.Write("未添加");
61                        }
62                        else
63                        {
64                                foreach (SchoolInfo info in list1)
65                                {
66                                        district = district + info.District + ",";
67                                }
68                                string strdistrict = district.Remove(district.Length - 1);
69
70                                foreach (SchoolInfo info in list2)
71                                {
72                                        school = school + info.School + ",";
73                                }
74                                string strschool = school.Remove(school.Length - 1);
75
76                                string s = strdistrict + "*" + strschool;
77                                Response.Write(s);
78                        }
79                }
80                if (Request.QueryString["district"] != null)
81                {
82                        IList<SchoolInfo> list = r.GetSchoolByDistrict(Request.QueryString["district"]);
83                        if (list.Count == 0)
84                        {
85                                Response.Write("未添加");
86                        }
87                        else
88                        {
89                                string schoolname = "";
90                                foreach (SchoolInfo info in list)
91                                {
92                                        schoolname = schoolname + info.School + ",";
93                                }
94                                string strschool = schoolname.Remove(schoolname.Length - 1);
95                                Response.Write(strschool);
96                        }
97                }
98        }
99}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章