ASP .NET DropDownList多级联动事件
来源:互联网 发布:江西在线网安全知让 编辑:程序博客网 时间:2024/05/21 05:42
思路
假如有三级省、市、区,先加载出所有省
选择省之后,加载出该省所有市
选择市之后,加载出该市所有区
重新选择省,则清空市和区
重新选择市,则清空区
想好数据结构,不同的数据结构做法不同
例子
数据结构
public class Area{ public int PKID { get; set; } public int ParentID { get; set; } public string Name { get; set; }}
测试数据
前台
<div> <span>地区搜索:</span>@Html.DropDownList("Provinces", new SelectList(ViewBag.Province as System.Collections.IEnumerable, "PKID", "Name"), "请选择") <select id="Citys"> <option value="">请选择</option> </select> <select id="Districts"> <option value="">请选择</option> </select> <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();}
0 0
- ASP .NET DropDownList多级联动事件
- ASP.net DropDownList 无刷新联动!
- ASP.net DropDownList 无刷新联动!
- ASP.NET实现无刷新DropDownList联动
- ASP.net DropDownList 无刷新联动菜单
- ASP.net DropDownList 无刷新联动菜单
- ASP.net DropDownList 无刷新联动菜单
- ASP.net DropDownList 无刷新联动
- ASP.net DropDownList 无刷新联动!
- ASP.net DropDownList 无刷新联动!
- ASP.net DropDownList 无刷新联动
- ASP.net DropDownList 无刷新联动菜单
- asp.net实现无刷新DropDownList联动
- asp.net DropDownList 三级联动下拉菜单
- 两个DropDownList的联动-----ASP.NET
- ASP 多级联动
- .net asp.net实现无刷新DropDownList联动控件
- yii 下拉列表多级联动 dropDownList
- 一只Android大三狗的2016
- C++Primer第五版 第五章习题答案(11~20)
- ROT13密码
- webview.cpp
- 史上最精炼Java基础知识点总结
- ASP .NET DropDownList多级联动事件
- hdu 1003 Max Sum
- Box2D v2.3.0 用户指南(第七章)
- Oracle列转行函数 Listagg() 语法详解及应用实例
- gdb调试(c++)
- Box2D v2.3.0 用户指南(第八章)
- Hadoop Yarn内存资源隔离实现原理——基于线程监控的内存隔离方案
- 【ife】任务二十六:行星与飞船(一)
- Box2D v2.3.0 用户指南(第九章)