實現地址聯動選擇

來源:互聯網
上載者:User

前台代碼,

<%@ 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;        }    }}此代碼還需要進一步修改

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.