$(document).ready(function () {
$("#Province").append("<option value=''>" + "--请选择--" + "</option>");
$("#City").append("<option value=''>" + "--请选择--" + "</option>");
$("#District").append("<option value=''>" + "--请选择--" + "</option>");
$.ajax({
url: "Handler1.ashx?type=0", //路径 地址
type: "get", //类型,post\get
dataType: "json", //json数据
success: function (msg) {//返回结果
for (var i = ; i < msg.length; i++) {
$("#Province").append("<option value='" + msg[i].ProvinceID + "'>" + msg[i].ProvinceName + "</option>");
}
}
});
//市
$("#Province").change(function () {
$("#City").empty(); //清空数据
$("#District").empty(); //清空数据
$("#District").append("<option value=''>" + "--请选择--" + "</option>");
var ProvinceID = $(this).val(); $.ajax({
url: "Handler1.ashx?type=1",
type: "get", //类型
data: {
ProvinceID: ProvinceID
},
dataType: "json", //json数据
success: function (msg) {
for (var i = ; i < msg.length; i++) { $("#City").append("<option value='" + msg[i].CityID + "'>" + msg[i].CityName + "</option>"); }
}
});
}); //区
$("#City").change(function () {
$("#District").empty(); //清空数据
var CityID = $(this).val();
$.ajax({
url: "Handler1.ashx?type=2",
type: "get", //类型
data: {
CityID: CityID
},
dataType: "json", //json数据
success: function (msg) {
for (var i = ; i < msg.length; i++) { $("#District").append("<option value='" + msg[i].DistrictID + "'>" + msg[i].DistrictName + "</option>"); }
}
}); });
}); <select id="Province"></select>
<select id="City"></select>
<select id="District" ></select>

结合ajax(一般处理程序)

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.Script.Serialization; namespace GovSystem.Company
{
/// <summary>
/// Handler1 的摘要说明
/// </summary>
public class Handler1 : IHttpHandler
{ public string text;
public void ProcessRequest(HttpContext context)
{
int action = Convert.ToInt32(context.Request.QueryString["type"]);
switch (action)
{
case (int)GetType.省:
text = Sel_Province(context);
break;
case (int) GetType.市:
text=Sel_City(context);
break;
case (int)GetType.区:
text = Sel_District(context);
break;
}
context.Response.ContentType = "text/plain";
context.Response.Write(text);
}
//省
private string Sel_Province(HttpContext context)
{
XieKe.BLL.S_Province b_Province = new XieKe.BLL.S_Province();
DataTable dt=b_Province.GetList("").Tables[];
List<XieKe.Model.S_Province> listInfo = new List<XieKe.Model.S_Province>();
for (int i = ; i < dt.Rows.Count; i++)
{
XieKe.Model.S_Province m_Province = new XieKe.Model.S_Province { ProvinceID = long.Parse(dt.Rows[i]["ProvinceID"].ToString()), DateCreated = Convert.ToDateTime(dt.Rows[i]["DateCreated"]), DateUpdated = Convert.ToDateTime(dt.Rows[i]["DateUpdated"]), ProvinceName = dt.Rows[i]["ProvinceName"].ToString() };
listInfo.Add(m_Province);
}
JavaScriptSerializer jssp = new JavaScriptSerializer();
string s= jssp.Serialize(listInfo);
return s;
}
//市
private string Sel_City(HttpContext context)
{
string ProvinceID = context.Request.QueryString["ProvinceID"];
XieKe.BLL.S_City b_City = new XieKe.BLL.S_City(); DataTable dt = b_City.GetList("ProvinceID="+ProvinceID).Tables[];
List<XieKe.Model.S_City> listInfo = new List<XieKe.Model.S_City>();
for (int i = ; i < dt.Rows.Count; i++)
{
XieKe.Model.S_City m_City = new XieKe.Model.S_City { CityID = long.Parse(dt.Rows[i]["CityID"].ToString()), CityName = dt.Rows[i]["CityName"].ToString(), DateCreated = Convert.ToDateTime(dt.Rows[i]["DateCreated"]), DateUpdated = Convert.ToDateTime(dt.Rows[i]["DateUpdated"]), ProvinceID = long.Parse(dt.Rows[i]["ProvinceID"].ToString()), ZipCode = dt.Rows[i]["ZipCode"].ToString() };
listInfo.Add(m_City);
}
JavaScriptSerializer jssp = new JavaScriptSerializer();
return jssp.Serialize(listInfo);
}
//区
private string Sel_District(HttpContext context)
{
string CityID = context.Request.QueryString["CityID"];
XieKe.BLL.S_District b_City = new XieKe.BLL.S_District();
DataTable dt = b_City.GetList("CityID=" + CityID).Tables[];
List<XieKe.Model.S_District> listInfo = new List<XieKe.Model.S_District>();
for (int i = ; i < dt.Rows.Count; i++)
{
XieKe.Model.S_District m_District = new XieKe.Model.S_District { CityID = Convert.ToInt32(dt.Rows[i]["CityID"]), DistrictName = dt.Rows[i]["DistrictName"].ToString() };
listInfo.Add(m_District);
}
JavaScriptSerializer jssp = new JavaScriptSerializer();
return jssp.Serialize(listInfo);
}
public bool IsReusable
{
get
{
return false;
}
}
public enum GetType
{
省 = ,
市 = ,
区 = ,
};
} }
05-11 15:20