Jquery代码

<script type="text/javascript">
          $(function(){

//提交事件
           $("#ImageButton1").click(function(){

//省Id
        var provinceId=$("#selProvince").val();

//市Id
        var cityId=$("#selCity").val();
        //区县Id

var districtId=$("#selDistrict").val();
        //把省市区ID赋值到隐藏域

$("#hidProvince").attr("value",provinceId);
         $("#hidCity").attr("value",cityId);
          $("#hidDistrict").attr("value",districtId);
          });
            //加载地区
         var area=$("#hidArea").val();  //后台将(省,市,区)ID放到隐藏域hidArea
//         alert(area);
         loadArea(0,0,area);//Ajax方法查询默认(北京,北京,朝阳)
         $("#selProvince").change(function(){//存放省的下拉框发生变化时调用Ajax
        loadArea(this.value,1,"");
        });
        $("#selCity").change(function(){//存放市的下拉框发生变化时调用Ajax
        loadArea(this.value,2,"");
         });

//id:省(0)市县id,type:0为省,1为市,2县,area:北京,北京,朝阳区
function loadArea(id,type,area){
    $.ajax({
        type:"get",
        url:"/ashx/GetArea.ashx",
        data:{Id:id,type:type},
        dataType:"json",
        success:function(data){
            var str="";
            var areaArr=area.split(",");
            if(type==0){
                $.each(data,function(i,item){
                    str=str+"<option value=\""+item.Id+"\">"+item.Name+"</option>";
                })
                var selProvince=$("#selProvince");
                selProvince.html(str);
                if(areaArr[0])
                {
                    selProvince.val(areaArr[0]);
                }
                loadArea(selProvince.val(),1,area);
            }
            if(type==1){
                $.each(data,function(i,item){
                    str=str+"<option value=\""+item.Id+"\">"+item.Name+"</option>";
                })
                var selCity=$("#selCity");
                selCity.html(str);
                if(areaArr[1])
                {
                    selCity.val(areaArr[1]);
                }
                loadArea(selCity.val(),2,area);
            }
            if(type==2){
                $.each(data,function(i,item){
                    str=str+"<option value=\""+item.Id+"\">"+item.Name+"</option>";
                })
                var selDistrict=$("#selDistrict");
                selDistrict.html(str);
                if(areaArr[2])
                {
                    selDistrict.val(areaArr[2]);
                }
                //$("#selDistrict")
            }
        }
        
    });
    }
        </script>


aspx页面代码

<div class="ptop1">填写联系地址:<span class="spanlv">*</span></div>
                <div class="ptop2">
                 <select id="selProvince" runat="server">
                </select>
                <select id="selCity" runat="server">
                </select>
                <select id="selDistrict" runat="server">
                </select>

<input type="hidden" id="hidArea" value="" runat="server" />
                <input type="hidden" runat="server" id="hidProvince" />
                <input type="hidden" runat="server" id="hidCity" />
                <input type="hidden" runat="server" id="hidDistrict" />
             
             </div>


一般处理程序 源码

using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using BLL;
using Newtonsoft.Json;
using System.Collections.Generic;
using Model;
using Common;
using Model.Linq;

namespace Web.ashx
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class GetArea : IHttpHandler
    {

public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int id = 0;
            string strId = context.Request.QueryString["Id"];
            //类型:省0,市1,县2
            string type = context.Request.QueryString["type"];
            if (!string.IsNullOrEmpty(strId))
            {
                id = Convert.ToInt32(strId);
            }
            phDataContext ph = new phDataContext();
            string result = "";
            if (type == "0")
            {
                result = ConvertHelper.DataToJson(ph.Province.Select(p=> new { p.Id, p.Name}));
                context.Response.Write(result);
            }
            if (type == "1")
            {
                result = ConvertHelper.DataToJson(ph.City.Where(c => c.ProvinceId == id).Select(c => new { c.Id,c.Name }));
                context.Response.Write(result);
            }
            if (type == "2")
            {
                result = ConvertHelper.DataToJson(ph.District.Where(c => c.CityId == id).Select(d => new { d.Id, d.Name }));
                context.Response.Write(result);
            }
            //context.Response.Write(result);
        }

public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}


后台代码

//这个就是给hidArea隐藏域赋值

if (!pm01.Area.IsNullOrEmptyOrWhiteSpace())
                    {
                        //this.hidArea.Value = pm01.Area;
                        hidArea.Value = pm01.ProvinceId + "," + pm01.CityId + "," + pm01.DistrictId;
                    }

自从用了ajax就停不下来了  (ˇˍˇ) 想~


Jquery+Ajax下拉框级联查询的更多相关文章

  1. ajax,下拉框级联

    js代码: $(document).ready(function() { $("#type1").change(function(){ var type1Code=$(" ...

  2. Java Swing应用程序 JComboBox下拉框联动查询

    在web项目中,通过下拉框.JQuery和ajax可以实现下拉框联动查询. 譬如说,当你查询某个地方时,页面上有:省份:<下拉框省份> 市区:<下拉框市区> 县乡:<下拉 ...

  3. jQuery对下拉框Select操作总结

    jQuery对下拉框Select操作总结 转自网络,留做备用 jQuery获取Select元素,并选择的Text和Value: 1. $("#select_id").change( ...

  4. layui select 下拉框 级联 动态赋值 与获取选中值

    //下拉框必须在 class="layui-form" 里 不然监听事件没有作用 <div class="layui-form" > <div ...

  5. jquery div 下拉框焦点事件

    这章与上一张<jquery input 下拉框(模拟select控件)焦点事件>类似 这章讲述div的焦点事件如何使用 div的焦点事件与input的焦点事件区别在于 需要多添加一个属性: ...

  6. Jquery操作下拉框(DropDownList)实现取值赋值

    Jquery操作下拉框(DropDownList)想必大家都有所接触吧,下面与大家分享下对DropDownList进行取值赋值的实现代码 1. 获取选中项: 获取选中项的Value值: $('sele ...

  7. jquery 获取下拉框值与select text

    下面先介绍了很多jquery获取select属性的方法,同时后面的实例我们讲的是jquery 获取下拉框值与select text代码. 下面先介绍了很多jquery获取select属性的方法,同时后 ...

  8. js,jquery获取下拉框选中的option

    js获取select选中的值: var sel=document.getElementById("select1"); var index = sel.selectedIndex; ...

  9. Jquery操作下拉框(DropDownList)的取值赋值实现代码(王欢)

    Jquery操作下拉框(DropDownList)的取值赋值实现代码(王欢) 1. 获取选中项: 获取选中项的Value值: $('select#sel option:selected').val() ...

随机推荐

  1. Python 3.0(一) 简介

    Python 3.0(一) 简介 [目录] 1.简介 2.python特点 3.安装 简介: Python是可以称得上即简单又功能强大的少有的语言中的一种.你将会惊喜地发现,专注于问题的解决方案而不是 ...

  2. oracle创建表相关

    --创建表 create table person( id number primary key, name ), birth date ); --创建序列 create sequence perso ...

  3. 二维码详解(QR Code)

    作者:王子旭链接:https://zhuanlan.zhihu.com/p/21463650来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 2016.7.5 更新:长文 ...

  4. VS自动生成的packages.config配置文件有什么用?

    通过nuget管理安装了程序包之后,Visual Studio会自动生成一个关于这些程序包版本的配置文件,删除或者保留它对程序不会造成什么影响.

  5. 从C#到Objective-C,循序渐进学习苹果开发(7)--使用FMDB对Sqlite数据库进行操作

    本随笔系列主要介绍从一个Windows平台从事C#开发到Mac平台苹果开发的一系列感想和体验历程,本系列文章是在起步阶段逐步积累的,希望带给大家更好,更真实的转换历程体验.本篇主要开始介绍基于XCod ...

  6. 控件使用经验-MVP模式+控件封装

    项目背景 几年前参与了一个面向学校的人事管理软件的开发,基于WinForm平台.今天主要想谈一谈其中关于控件的使用经验.这个项目我们大量使用了第三方控件.由于这个产品的生命周期很长,我们在设计时要考虑 ...

  7. Nancy FormsAuthentication使用

    1.新建UserDatabase类,实现IUserMapper接口 using System; using System.Collections.Generic; using System.Linq; ...

  8. Asp.net中使用Server.HtmlDecode(string str)的使用

    前言: 在使用Visual Studio开发web页面时,需要在GridView中绑定Table数据,并加入了CommandField, 试图,点击详情按钮是,获取GridView中Rows中Cell ...

  9. css3渐变(Gradients)

    <html> http://www.runoob.com/css3/css3-gradients.html CSS3 渐变(gradients)可以让你在两个或多个指定的颜色之间显示平稳的 ...

  10. 2015暑假多校联合---Expression(区间DP)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5396 Problem Description Teacher Mai has n numb ...