思路

假如有三级省、市、区,先加载出所有省
选择省之后,加载出该省所有市
选择市之后,加载出该市所有区
重新选择省,则清空市和区
重新选择市,则清空区
想好数据结构,不同的数据结构做法不同

例子

数据结构

public class Area
{
public int PKID { get; set; }
public int ParentID { get; set; }
public string Name { get; set; }
}

测试数据

 
1

前台

<div>
<span>地区搜索:</span>@Html.DropDownList("Provinces", new SelectList(ViewBag.Province as System.Collections.IEnumerable, "PKID", "Name"), "请选择")
&nbsp;&nbsp;&nbsp;&nbsp;
<select id="Citys">
<option value="">请选择</option>
</select>
&nbsp;&nbsp;&nbsp;&nbsp;
<select id="Districts">
<option value="">请选择</option>
</select>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<button onclick="GetResult()">获取当前选择</button>
</div>
<script>
$("#Provinces").change(function () {
var self = $(this);
var parentId = self.val();
if (parentId != "") {
$("#Province").val(self.find("option:selected").val());
var option = GetRegion(parentId);
$("#Citys").html(option);
$("#Districts").html("<option value=''>请选择</option>");
} else {
$("#Citys").html("<option value=''>请选择</option>");
$("#Districts").html("<option value=''>请选择</option>");
}
});
$("#Citys").change(function () {
var self = $(this);
var parentId = self.val();
if (parentId != "") {
$("#City").val(self.find("option:selected").val());
$("#RegionID").val(parentId);
var option = GetRegion(parentId);
$("#Districts").html(option);
} else {
$("#Districts").html("<option value=''>请选择</option>");
}
});
function GetRegion(ParentID) {
var option = "<option value=''>请选择</option>";
$.ajax({
type: "get",
url: "/AboutDB/GetArea",
data: { "ParentID": ParentID },
async: false,
success: function (city) {
//拼接下拉框
$.each(city, function (index, item) {
option += "<option value=" + item.PKID + ">" + item.Name + "</option>";
});
}
});
//返回下拉框html
return option;
}
function GetResult()
{
var Province = $("#Provinces").find("option:selected").text();
var City = $("#Citys").find("option:selected").text();
var District = $("#Districts").find("option:selected").text();
layer.alert(Province + "-" + City + "-" + District);
}
</script>

后台

//加载页面,先查出省列表
public ActionResult Area()
{
ViewBag.Province = new AboutDBManager().GetArea(0);
return View();
}
//根据ParentID查询子集
public ActionResult GetArea(int ParentID)
{
var regions = new AboutDBManager().GetArea(ParentID);
return Json(regions, JsonRequestBehavior.AllowGet);
}
public List<Area> GetArea(int ParentID)
{
string sql =string.Format("select PKID,ParentID,Name from area where ParentID={0}",ParentID);
return DAL.DbManager<Area>.Instance.QueryBySQL(sql).ToList();
}

ASP .NET DropDownList多级联动事件的更多相关文章

  1. DropDownList的多级联动

    DropDownList的多级联动的问题最典型的案例就是实现省市级三级联动的案例,对这个问题的描述是当选中山东省之后,在选择市的下拉菜单时,市一栏只出现山东省下面的市.对于县也是同样的道理. 我也做的 ...

  2. asp.net MVC 下拉多级联动及编辑

    多级联动实现,附源码.当前,部分代码是参与博客园其它网友. 新增,前台代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 2 ...

  3. vue在多级联动时,一些情况不用watch而用onchange会更好

    onchange事件在内容改变且失去焦点时触发,因此在一些多级联动需要清空次级内容的时候,用onchange就非常有用了,尤其是浏览器会提前加载数据的情况下.有篇文章可以看一下,链接. PS:路漫漫其 ...

  4. Asp.net DropDownList 自定义样式(想怎么改就怎么改!)

    最近在做一个asp.net的项目,需要对默认的dropdownlist样式进行美化,固有的dropdownlist的小箭头实在让人无法接受,于是开始在百度,google 上下求索,天不负有心人,终于找 ...

  5. 为什么DropDownList的SelectedIndexChanged事件触发不了

    写的还行,转来大家看看 为什么DropDownList的SelectedIndexChanged事件触发不了? 为什么设置了DropDownList的AutoPostBack="True&q ...

  6. asp.net学习之GridView事件、GridViewRow对象

    原文:asp.net学习之GridView事件.GridViewRow对象 1. GridView控件的事件 GridView有很多事件,事件可以定制控件的外观或者行为.事件分为三类     1.1 ...

  7. JavaScript 多级联动浮动(下拉)菜单 (第二版)

    JavaScript 多级联动浮动(下拉)菜单 (第二版)   上一个版本(第一版请看这里)基本实现了多级联动和浮动菜单的功能,但效果不是太好,使用麻烦还有些bug,实用性不高.这次除了修改已发现的问 ...

  8. MVC实现多级联动

    前言 多级联动(省级联动)的效果,网上现成的都有很多,各种JS实现,Jquery实现等等,今天我们要讲的是在MVC里面,如何更方便.更轻量的实现省级联动呢? 实现效果如下: 具体实现 如图所示,在HT ...

  9. js 多级联动(省、市、区)

      js 多级联动(省.市.区) CreateTime--2018年4月9日17:10:38 Author:Marydon 方式一: 数据从数据库获取,ajax实现局部刷新 方式二: 数据从json文 ...

随机推荐

  1. 终端中经常使用的shell 命令

    Mac 在shell命令终端中,Ctrl+n相当于方向向下的方向键,Ctrl+p相当于方向向上的方向键. 在命令终端中通过它们或者方向键能够实现对历史命令的高速查找.这也是高速输入命令的技巧. 在命令 ...

  2. 远离“精神乞丐”(IBM的前CEO郭士纳把员工分为四种类型)

    语音丨吴伯凡 乞丐与其说是一种身份, 不如说是一种精神状态, 习惯性索取且心安理得, 习惯性寻求安慰,习惯性抱怨, 与之截然对立的, 是“操之在我”(Proactive)的精神, 乞丐型员工是公司内部 ...

  3. 【26.8%】【CF 46D】Parking Lot

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  4. Net Reactor 5

    Net Reactor 5脱壳教程   今天别人发来一个.Net的DLL让我脱壳,第一步自然是先扔进de4dot 我这个de4dot 是集成了  Ivancito0z / TheProxy / PC- ...

  5. [TypeScript] Catch unsafe use of "this" in TypeScript functions

    this is probably the most tricky thing to use in JavaScript and therefore TypeScript. Fortunately th ...

  6. poj 2689 Prime Distance(大区间筛素数)

    http://poj.org/problem?id=2689 题意:给出一个大区间[L,U],分别求出该区间内连续的相差最小和相差最大的素数对. 由于L<U<=2147483647,直接筛 ...

  7. ECharts.js 超简单入门(本质canvas)

    ECharts.js 超简单入门(本质canvas) 一.总结 一句话总结:echarts这些图标的本质都是canvas. 二.ECharts.js学习(一) 简单入门 EChart.js 简单入门 ...

  8. 【u032】均衡发展

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 神牛小R在许多方面都有着很强的能力,具体的说,他总共有m种能力,并将这些能力编号为1到m.他的能力是一 ...

  9. 【t082】牛跑步

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 新牛到部队,CG要求它们每天早上搞晨跑,从A农场跑到B农场.从A农场到B农场中有n-2个路口,分别标上 ...

  10. 避免if语句的深层次嵌套

    公司做了个抢红包的限制,然后ajax请求的返回字段,要进行多层逻辑的判断,想想是真恶心,虽然都是简单逻辑,而且看别人以前写的代码,发现,哎,注释能不能写上吶,像我写代码都是细致到,哪怕初学者也能看懂这 ...