思路

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

例子

数据结构

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. iOS开发网络学习七:NSURLSession的基本使用get和post请求

    #import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...

  2. 【机器学习实战】第7章 集成方法(随机森林和 AdaBoost)

    第7章 集成方法 ensemble method 集成方法: ensemble method(元算法: meta algorithm) 概述 概念:是对其他算法进行组合的一种形式. 通俗来说: 当做重 ...

  3. RESET MASTER 和RESET SLAVE 命令的使用方法 注意事项

    RESET MASTER 删除所有index file 中记录的所有binlog 文件,将日志索引文件清空,创建一个新的日志文件,这个命令通常仅仅用于第一次用于搭建主从关系的时的主库,注意  rese ...

  4. ios UI控件的简单整理

    把该文件拷贝到.m文件中就能够方便的查找 /** 匿名类目:能够声明方法和变量,属性为private(不同意在外部调用,且不能被继承 */ /** 发送数据的托付方,接收数据的时代理发(即代理的反向传 ...

  5. 日志报错Can't add self as subview

    #pragma mark- add 20151112 导航动画时间太短导致崩溃,重写UINavigationcontroller以下相关方法 - (id)navigationLock; ///< ...

  6. Ubuntu-Docker[1]安装Docker,通过Docker部署net core代码,需要结合[.NET Core 18]发布、ASP.NET Core Docker部署

    1)通过系统自带包安装 通过自带包安装,可能Docker版本较旧 $ sudo apt-get update Reading package lists... Done $ sudo apt-get ...

  7. findbugs静态代码分析工具使用教程

    FindBugs 是一个静态分析工具,很多程序猿都在使用,再次详细列出findbugs的使用教程,希望对大家有帮助. 1 安装 FindBugs通过检查类文件或 JAR文件,将字节码与一组缺陷模式进行 ...

  8. VB的MSHFlexGrid控件内容导入Excel

    机房收费系统中有非常多窗口用到导出到Excel,说一下vb与Excel的交互,怎样才干将MSHFlexgrid中的内容导出到Excel. 首先在VB中加入引用Microsoft Excel 14.0 ...

  9. 【a202】&&【9208】输油管道问题

    Time Limit: 10 second Memory Limit: 2 MB 问题描述 某石油公司计划建造一条由东向西的主输油管道.该管道要穿过一个有n 口油井的油 田.从每口油井都要有一条输油管 ...

  10. Methods for Using Message Queuing Telemetry Transport for Sensor Networks to Support Sleeping Devices

    Methods support a sleep mode for an embedded device. Embedded devices like sensors and actuators use ...