级联查询ajax,Jquery+Ajax下拉框级联查询

786969h0tLT4kcAKUwGaml+8j5l4xlT840zXq9XqlU7cIVCrura+vPnv29vJx99Gh5YWFxbm7+yZNlIgdAKf8DLAsVHzEZfSIAAAAASUVORK5CYII=

Jquery代码

$(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+""+item.Name+"";

})

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+""+item.Name+"";

})

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+""+item.Name+"";

})

var selDistrict=$("#selDistrict");

selDistrict.html(str);

if(areaArr[2])

{

selDistrict.val(areaArr[2]);

}

//$("#selDistrict")

}

}

});

}

aspx页面代码

填写联系地址: *

一般处理程序 源码

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

{

///

/// $codebehindclassname$ 的摘要说明

///

[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就停不下来了  (ˇ?ˇ) 想~

原文:http://www.cnblogs.com/LOVEJIEYING/p/5498972.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值