jquery ajax 通过json取值并且进行联动

前台页面:

JS 部分:(这里的js如果单独写为一个js文件,貌似联动不起作用,所以建议大家还是放在页面里面(html、aspx))

   
   

 

html部分:

   企业性质


   

    行业类型

   

 

IndexHandler.ashx部分:(我这里只是一个简单的数据绑定取值,当然也可以从数据库取得数据,具体取数据查看项目实际情况)

  public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";
            //接收提交过来的meth参数
            string meth = context.Request.Params["meth"].ToString();
            string pid = context.Request.Params["pid"].ToString();
            //根据参数调用不同的方法
            switch (meth)
            {
                case "load":
                    loadjson(context, pid);
                    break;
            }
        }

        private void loadjson(HttpContext context, string pid)
        {
            //实例BLL
            List areaList = new List();

            if (pid == "0")
            {
                areaList.Add(new Area
                {
                    AreaID = "01",
                    AreaName = "罗湖区",
                    PID = "0"
                });
                areaList.Add(new Area
                {
                    AreaID = "02",
                    AreaName = "福田区",
                    PID = "0"
                });
            }

            if (pid == "01")
            {
                areaList.Add(new Area
                {
                    AreaID = "0101",
                    AreaName = "莲塘",
                    PID = "01"
                });
                areaList.Add(new Area
                {
                    AreaID = "0102",
                    AreaName = "黄贝岭",
                    PID = "01"
                });
            }
            if (pid == "02")
            {
                areaList.Add(new Area
                {
                    AreaID = "0201",
                    AreaName = "八卦岭",
                    PID = "02"
                });
                areaList.Add(new Area
                {
                    AreaID = "0202",
                    AreaName = "华强",
                    PID = "02"
                });
            }

            //实例一个StringBuilder 用来拼接一个json数据
            StringBuilder sbvoca = new StringBuilder();

            if (areaList != null && areaList.Count > 0)
            {
                sbvoca.Append("{\"voce\":[");
                int i = 1;
                int count = areaList.Count;
                foreach (Area dr in areaList)
                {
                    if (i == count)
                    {
                        sbvoca.Append("{\"code\":\"" + dr.AreaID + "\",\"name\":\"" + dr.AreaName + "\"}");
                    }
                    else
                    {
                        sbvoca.Append("{\"code\":\"" + dr.AreaID + "\",\"name\":\"" + dr.AreaName + "\"},");
                    }
                    i++;
                }
                sbvoca.Append("]");
            }
            sbvoca.Append("}");
            context.Response.Write(sbvoca.ToString());

            context.Response.End();
        }

        public class Area
        {
            public string AreaID { get; set; }
            public string AreaName { get; set; }
            public string PID { get; set; }
        }

 

你可能感兴趣的:(JQuery)