前台代碼,
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AddressSelect.aspx.cs" Inherits="UI_AddressSelect" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>地址聯動選擇-by wangtao</title>
<script type="text/javascript" src="../Misc/Js/Core/Jquery.js"></script>
</head>
<body>
<script type="text/javascript">
function ajaxGetAdress(parms,Containers)
{
$.ajax({
url: '../AjaxServices/GetAddress.ashx',
type: 'GET',
dataType:'xml',
data:parms,
error: function(){
alert('Error loading XML document');
},
success:function(xml)
{
alert(xml);
$(xml).find("Item").each(function(){
var item='<option value=/"'+$(this).find('value').text()+'">'+$(this).find('text').text()+'</option>';
$(Containers).append(item);
});
}
})
}
$(function(){
$("#country").change(function(){
var geoCode=$(this).val();
alert(geoCode);
var type='country';
var parms='type='+type+'&geoCode='+geoCode;
var Containers='#province';
ajaxGetAdress(parms,Containers);
})
$("#province").change(function(){
var code=$(this).val();
alert(code);
});
$("#city").change(function(){
var code=$(this).val();
alert(code);
});
})
</script>
<form id="addressSelectform" runat="server">
<div>
<select id="country">
<option value="china">china</option>
<option value="can">can</option>
</select>
<select id="province">
<option value="shichuan">shichuan</option>
<option value="cq">chongqin</option>
</select>
<select id="city">
<option value="chongqin">shapingba</option>
<option value="chendu">chendu</option>
</select>
</div>
</form>
</body>
</html>
後台代碼<%@ WebHandler Language="C#" Class="GetAddress" %>using System;using System.Web;using System.Xml;public class GetAddress : IHttpHandler { public void ProcessRequest (HttpContext context) { context.Response.Charset = "utf-8"; context.Response.ContentType = "text/xml"; string type = string.IsNullOrEmpty(context.Request.Params["type"]) ? string.Empty : context.Request.Params["type"]; string geoCode = string.IsNullOrEmpty(context.Request.Params["geoCode"]) ? string.Empty : context.Request.Params["geoCode"]; // XmlDocument doc = new XmlDocument(); string data = string.Empty;//類比從資料庫出來的資料 if (string.IsNullOrEmpty(type)) { ///可通過type尋找到相應的下一級的資料 switch (type) { case "country": data = geoCode + "_shichuan"; break; case "province": data = geoCode + "_chendu"; break; case "city": data = geoCode + "_huyang"; break; } } string strXml = string.Format(" <?xml version=/"1.0/" encoding=/"utf-8/"?> <Root><Item> <value>{0}</value><text>{1}</text><Item></Root>", data, data); context.Response.Write(strXml); //doc.a } public bool IsReusable { get { return false; } }}此代碼還需要進一步修改