博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ajax省市县联动
阅读量:5909 次
发布时间:2019-06-19

本文共 5908 字,大约阅读时间需要 19 分钟。

procitysel.htm

 

                     

 

 

procitysel.js

//创兼容所以游览器的建异步请求对象function CreateXhr() {    var xhrobj = false;    try {        xhrobj = new ActiveXObject("Msxml2.XMLHTTP");    }    catch (e) {        try {            xhrobj = new ActiveXObject("Microsoft.XMLHTTP");        }        catch (e1) {            xhrobj = false;        }    }    if (!xhrobj && typeof XMLHttpRequest != "underfined") {        //创建异步以求对象        xhrobj = new XMLHttpRequest();    }    return xhrobj;}var xhr = CreateXhr();window.onload = function () {    loadPr();    document.getElementById("proSel").onchange = function () {    selectcity(document.getElementById("proSel").value);    }    document.getElementById("citySel").onchange = function () {        selectDe(document.getElementById("citySel").value);    }}function loadPr() {    xhr.open("Get", "procitysel.ashx?type=1", true);    //   xhr.setRequestHeader("If-ModeFied-Since", "0")    xhr.onreadystatechange = function () {        if (xhr.readyState == 4) {            if (xhr.status == 200) {                var jsonList = eval(xhr.responseText);                var proSel = document.getElementById("proSel");                // alert(jsonList[1].prName)                for (var i = 0; i < jsonList.length; i++) {                    proSel.options[i] = new Option(jsonList[i].prName, jsonList[i].prId);                }                selectcity(document.getElementById("proSel").value);            }        }    }    xhr.send(null);}function selectcity(proSelId) {    document.getElementById("citySel").length = 0;    document.getElementById("deSel").length = 0;    xhr.open("Get", "procitysel.ashx?type=2&proId=" + proSelId + "", true);    //   xhr.setRequestHeader("If-ModeFied-Since", "0")    xhr.onreadystatechange = function () {        if (xhr.readyState == 4) {            if (xhr.status == 200) {                var jsonList = eval(xhr.responseText);                var citySel = document.getElementById("citySel");                // alert(jsonList[1].prName)                for (var i = 0; i < jsonList.length; i++) {                    citySel.options[i] = new Option(jsonList[i].cName, jsonList[i].cId);                }                selectDe(document.getElementById("citySel").value);            }        }    }    xhr.send(null);}function selectDe(dId) {    xhr.open("Get", "procitysel.ashx?type=3&dId=" + dId + "", true);    // xhr.setRequestHeader("If-ModeFied-Since", "0")    xhr.onreadystatechange = function () {        if (xhr.readyState == 4) {            if (xhr.status == 200) {             //  alert(xhr.responseText);                try {                    var jsonList = eval(xhr.responseText);                }                catch (e) {                    return;                }                var deSel = document.getElementById("deSel");                // alert(jsonList[1].prName)                for (var i = 0; i < jsonList.length; i++) {                    deSel.options[i] = new Option(jsonList[i].dName, jsonList[i].dId);                }            }        }    }    xhr.send(null);}

procitysel.ashx

 

<%@ WebHandler Language="C#" Class="procitysel" %>using System;using System.Web;using System.Data.SqlClient;using System.Configuration;public class procitysel : IHttpHandler{    public void ProcessRequest(HttpContext context)    {        context.Response.ContentType = "text/plain";        string type = (string)context.Request.QueryString["type"];        string connStr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;        SqlConnection conn = new SqlConnection(connStr);        System.Text.StringBuilder sb = new System.Text.StringBuilder();        if (type == "1")        {            conn.Open();            SqlCommand cmd = new SqlCommand();            cmd.Connection = conn;            cmd.CommandText = "select * from  dbo.T_Province";            SqlDataReader dr = cmd.ExecuteReader();            sb.Append("[");            while (dr.Read())            {                sb.Append("{prId:" + (string)dr[0].ToString() + ",prName:'" + (string)dr[1] + "'},");            }            sb.Append("]");            conn.Close();            dr.Close();            string strHtml=sb.ToString().Remove(sb.Length-2,1);            context.Response.Write(strHtml);        }        else if (type == "2")        {            conn.Open();            string proId = (string)context.Request.QueryString["ProId"];            SqlCommand cmd = new SqlCommand();            cmd.Connection = conn;            cmd.CommandText = "select * from  dbo.T_City where ProID=" + proId + "";            SqlDataReader dr = cmd.ExecuteReader();            sb.Append("[");            while (dr.Read())            {                sb.Append("{cId:" + (string)dr[0].ToString() + ",cName:'" + (string)dr[1] + "'},");            }            sb.Append("]");            conn.Close();            dr.Close();            string strHtml = sb.ToString().Remove(sb.Length - 2, 1);            context.Response.Write(strHtml);        }        else if (type == "3")        {            conn.Open();            string dId = (string)context.Request.QueryString["dId"];            SqlCommand cmd = new SqlCommand();            cmd.Connection = conn;            cmd.CommandText = "select * from  dbo.T_District where CityID=" + dId + "";            SqlDataReader dr = cmd.ExecuteReader();            sb.Append("[");            while (dr.Read())            {                sb.Append("{dId:" + (string)dr[0].ToString() + ",dName:'" + (string)dr[1] + "'},");            }            sb.Append("]");            conn.Close();            dr.Close();            string strHtml = sb.ToString().Remove(sb.Length - 2, 1);            context.Response.Write(strHtml);        }    }    public bool IsReusable    {        get        {            return false;        }    }}

 

数据库

 数据库上传不上来呀,

 

 

转载于:https://www.cnblogs.com/zsss/archive/2013/01/07/2849862.html

你可能感兴趣的文章
一位运营该做的事情
查看>>
IQ Test
查看>>
打开系统设置
查看>>
[stm32] STM32 Interrupts and events 系统了解(EXTI)及槽型光电开关tp850电路研究
查看>>
PCA的数学原理
查看>>
大学溃败始于老实人吃亏
查看>>
实现关闭ssh继续实施方案---tmux
查看>>
滴滴快车奖励政策,高峰奖励,翻倍奖励,按成交率,指派单数分级(7月23日)...
查看>>
iSight集成Adams/View:Adams组件
查看>>
HDU 5063 Operation the Sequence
查看>>
android安卓系统上运行jar文件
查看>>
HttpClient使用线程锁synchronized
查看>>
(高德地图)marker定位 bug 解决总结
查看>>
cocos2dx-3.1加入cocosStudio参考库 libCocosStudio
查看>>
堆表和%%lockres%%函数
查看>>
Android 摇一摇 之 震动片
查看>>
语言学习目标
查看>>
微信公众平台开发教程(七)安全策略
查看>>
Visual Studio 添加SVN插件
查看>>
BZOJ [ZJOI2008]泡泡堂BNB 贪心
查看>>