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;
}
}
}