您现在的位置是:首页 > 文章详情

常用省市区无刷新联动实例

日期:2014-10-22点击:601

1、jquery代码

function getCity(CityVal) { var DDL_Province = $("#DDL_Province"); var DDL_City = $("#DDL_City"); DDL_City.empty(); DDL_City.append("<option value=\"0\">市/区</option>"); $.ajax( { type: "post", url: "/UserCart/Controller/CityAreas.ashx", data: { "type": "city", "provinceID": DDL_Province.val() }, dataType: "json", async: false, success: function (msg) { for (var i = 0; i < msg.length; i++) { if (CityVal == msg[i].CityName) { if (msg[i].IsCOD == 1) { DDL_City.append("<option value=" + msg[i].CityID + " selected=\"selected\">" + msg[i].CityName + "*</option>"); } else { DDL_City.append("<option value=" + msg[i].CityID + " selected=\"selected\">" + msg[i].CityName + "</option>"); } } else { if (msg[i].IsCOD == 1) { DDL_City.append("<option value=" + msg[i].CityID + " >" + msg[i].CityName + "*</option>"); } else { DDL_City.append("<option value=" + msg[i].CityID + " >" + msg[i].CityName + "</option>"); } } } getArea(''); GetAddreesSpan(); } }) }; function getArea(AreaVal) { var DDL_City = $("#DDL_City"); var DDL_Area = $("#DDL_Area"); DDL_Area.empty(); DDL_Area.append("<option value=\"0\">县/乡</option>"); $.ajax( { type: "post", url: "/UserCart/Controller/CityAreas.ashx", data: { "type": "district", "cityID": DDL_City.val() }, dataType: "json", async: false, success: function (msg) { for (var i = 0; i < msg.length; i++) { if (AreaVal == msg[i].DistrictName) { if (msg[i].IsCOD == 1) { DDL_Area.append("<option value=" + msg[i].DistrictID + " selected=\"selected\">" + msg[i].DistrictName + "*</option>"); } else { DDL_Area.append("<option value=" + msg[i].DistrictID + " selected=\"selected\">" + msg[i].DistrictName + "</option>"); } } else { if (msg[i].IsCOD == 1) { DDL_Area.append("<option value=" + msg[i].DistrictID + " >" + msg[i].DistrictName + "*</option>"); } else { DDL_Area.append("<option value=" + msg[i].DistrictID + " >" + msg[i].DistrictName + "</option>"); } } } GetAddreesSpan(); } }) }; function GetAddreesSpan() { } 


 

2、后端C#代码

<%@ WebHandler Language="C#" Class="CityAreas" %> using System; using System.Web; using System.Collections.Generic; public class CityAreas : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; if (context.Request["type"] == "city") { context.Response.Write(select2(context.Request["provinceID"])); } else if (context.Request["type"] == "district") { context.Response.Write(select3(context.Request["cityID"])); } } public string select2(string id) { string str = string.Empty; if (!string.IsNullOrEmpty(id)) { List<ECS.Model.A_CityAreas> list = new ECS.BLL.A_CityAreas().GetList(null, "deep=2 and ParentID=" + id, null); if (list != null && list.Count > 0) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append("["); foreach (ECS.Model.A_CityAreas item in list) { sb.Append("{"); sb.Append("\"CityID\":\"" + item.id + "\",\"CityName\":\"" + item.AreaName + "\",\"IsCOD\":\"" + item.IsCOD + "\""); sb.Append("},"); } sb.Remove(sb.Length - 1, 1); sb.Append("]"); str = sb.ToString(); } } return str; } public string select3(string id) { string str = string.Empty; if (!string.IsNullOrEmpty(id)) { List<ECS.Model.A_CityAreas> list = new ECS.BLL.A_CityAreas().GetList(null, "deep=3 and ParentID=" + id, null); if (list != null && list.Count > 0) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append("["); foreach (ECS.Model.A_CityAreas item in list) { sb.Append("{"); sb.Append("\"DistrictID\":\"" + item.id + "\",\"DistrictName\":\"" + item.AreaName + "\",\"IsCOD\":\"" + item.IsCOD + "\""); sb.Append("},"); } sb.Remove(sb.Length - 1, 1); sb.Append("]"); str = sb.ToString(); } } return str; } public bool IsReusable { get { return false; } } }


 

原文链接:https://yq.aliyun.com/articles/248178
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章