//author:guan
//2015-05-25
//省市联动
//实用说明,页面引用如下js
//<script src="../js/jquery-1.6.3.min.js"></script>
//<script src="js/ProvinceDropDown.js"></script>
//页面元素
//<select id="Province"></select>
//<select id="City"></select> $(function () {
//初始化下拉框值
InitDropDown("Province", "City");
})
var Dropdowndata = [{ "ParentName": "宁夏", "ParentCode": "1", "ChildrenNodes": [{ "ChildrenName": "银川", "ChildrenCode": "11" }] }, { "ParentName": "山东", "ParentCode": "1", "ChildrenNodes": [{ "ChildrenName": "济南", "ChildrenCode": "11" }, { "ChildrenName": "青岛", "ChildrenCode": "12" }, { "ChildrenName": "潍坊", "ChildrenCode": "13" }] }, { "ParentName": "甘肃", "ParentCode": "1", "ChildrenNodes": [{ "ChildrenName": "兰州", "ChildrenCode": "11" }] }, { "ParentName": "天津", "ParentCode": "1", "ChildrenNodes": [{ "ChildrenName": "天津", "ChildrenCode": "11" }] }, { "ParentName": "湖南", "ParentCode": "1", "ChildrenNodes": [{ "ChildrenName": "长沙", "ChildrenCode": "11" }] }, { "ParentName": "湖北", "ParentCode": "1", "ChildrenNodes": [{ "ChildrenName": "武汉", "ChildrenCode": "11" }, { "ChildrenName": "襄阳", "ChildrenCode": "12" }] }, { "ParentName": "云南", "ParentCode": "1", "ChildrenNodes": [{ "ChildrenName": "昆明", "ChildrenCode": "11" }] }, { "ParentName": "广东", "ParentCode": "1", "ChildrenNodes": [{ "ChildrenName": "广州", "ChildrenCode": "11" }, { "ChildrenName": "深圳", "ChildrenCode": "12" }, { "ChildrenName": "佛山", "ChildrenCode": "13" }, { "ChildrenName": "东莞", "ChildrenCode": "14" }, { "ChildrenName": "中山", "ChildrenCode": "15" }] }, { "ParentName": "浙江", "ParentCode": "1", "ChildrenNodes": [{ "ChildrenName": "杭州", "ChildrenCode": "11" }, { "ChildrenName": "宁波", "ChildrenCode": "12" }, { "ChildrenName": "嘉兴", "ChildrenCode": "13" }, { "ChildrenName": "绍兴", "ChildrenCode": "14" }, { "ChildrenName": "义乌", "ChildrenCode": "15" }, { "ChildrenName": "温州", "ChildrenCode": "16" }, { "ChildrenName": "台州", "ChildrenCode": "17" }] }, { "ParentName": "江西", "ParentCode": "1", "ChildrenNodes": [{ "ChildrenName": "南昌", "ChildrenCode": "11" }] }, { "ParentName": "新疆", "ParentCode": "1", "ChildrenNodes": [{ "ChildrenName": "乌鲁木齐", "ChildrenCode": "11" }] }, { "ParentName": "北京", "ParentCode": "1", "ChildrenNodes": [{ "ChildrenName": "北京", "ChildrenCode": "11" }] }, { "ParentName": "河北", "ParentCode": "1", "ChildrenNodes": [{ "ChildrenName": "唐山", "ChildrenCode": "11" }, { "ChildrenName": "保定", "ChildrenCode": "12" }, { "ChildrenName": "邯郸", "ChildrenCode": "13" }] }, { "ParentName": "贵州", "ParentCode": "1", "ChildrenNodes": [{ "ChildrenName": "贵阳", "ChildrenCode": "11" }] }, { "ParentName": "四川", "ParentCode": "1", "ChildrenNodes": [{ "ChildrenName": "成都", "ChildrenCode": "11" }, { "ChildrenName": "南充", "ChildrenCode": "12" }, { "ChildrenName": "泸州", "ChildrenCode": "13" }, { "ChildrenName": "宜宾", "ChildrenCode": "14" }, { "ChildrenName": "绵阳", "ChildrenCode": "15" }] }, { "ParentName": "吉林", "ParentCode": "1", "ChildrenNodes": [{ "ChildrenName": "长春", "ChildrenCode": "11" }] }, { "ParentName": "安徽", "ParentCode": "1", "ChildrenNodes": [{ "ChildrenName": "合肥", "ChildrenCode": "11" }, { "ChildrenName": "芜湖", "ChildrenCode": "12" }] }, { "ParentName": "黑龙江", "ParentCode": "1", "ChildrenNodes": [{ "ChildrenName": "哈尔滨", "ChildrenCode": "11" }] }, { "ParentName": "辽宁", "ParentCode": "1", "ChildrenNodes": [{ "ChildrenName": "沈阳", "ChildrenCode": "11" }, { "ChildrenName": "大连", "ChildrenCode": "12" }] }, { "ParentName": "山西", "ParentCode": "1", "ChildrenNodes": [{ "ChildrenName": "山西", "ChildrenCode": "11" }] }, { "ParentName": "河南", "ParentCode": "1", "ChildrenNodes": [{ "ChildrenName": "郑州", "ChildrenCode": "11" }] }, { "ParentName": "福建", "ParentCode": "1", "ChildrenNodes": [{ "ChildrenName": "福州", "ChildrenCode": "11" }, { "ChildrenName": "厦门", "ChildrenCode": "12" }, { "ChildrenName": "泉州", "ChildrenCode": "13" }] }, { "ParentName": "重庆", "ParentCode": "1", "ChildrenNodes": [{ "ChildrenName": "重庆", "ChildrenCode": "11" }] }, { "ParentName": "江苏", "ParentCode": "1", "ChildrenNodes": [{ "ChildrenName": "南京", "ChildrenCode": "11" }, { "ChildrenName": "苏州", "ChildrenCode": "12" }, { "ChildrenName": "无锡", "ChildrenCode": "13" }, { "ChildrenName": "常州", "ChildrenCode": "14" }, { "ChildrenName": "南通", "ChildrenCode": "15" }, { "ChildrenName": "扬州", "ChildrenCode": "16" }, { "ChildrenName": "盐城", "ChildrenCode": "17" }] }, { "ParentName": "陕西", "ParentCode": "1", "ChildrenNodes": [{ "ChildrenName": "西安", "ChildrenCode": "11" }, { "ChildrenName": "榆林", "ChildrenCode": "12" }] }, { "ParentName": "广西", "ParentCode": "1", "ChildrenNodes": [{ "ChildrenName": "南宁", "ChildrenCode": "11" }] }, { "ParentName": "海南", "ParentCode": "1", "ChildrenNodes": [{ "ChildrenName": "海口", "ChildrenCode": "11" }] }, { "ParentName": "上海", "ParentCode": "1", "ChildrenNodes": [{ "ChildrenName": "上海", "ChildrenCode": "11" }] }]; function InitDropDown(province, city) {
var _province = $("#" + province);
//初始化清空,为其添加请选择默认选项
_province.empty().append($("<option>").text("请选择").val("-1")); var _city = $("#" + city);
_city.empty().append($("<option>").text("请选择").val("-1"));
//循环读取数组的json一级下拉值
for (var i = 0; i < Dropdowndata.length; i++) {
var option = $("<option>").text(Dropdowndata[i].ParentName).val(Dropdowndata[i].ParentCode).attr("pos", i);
_province.append(option);
}
$(_province).change(function () {
_city.empty().append($("<option>").text("请选择").val("-1"));
//根据数组下标从数据取出json数据
var citys = Dropdowndata[$(this).find("option:selected").attr("pos")].ChildrenNodes;
for (var i = 0; i < citys.length; i++) {
var option = $("<option>").text(citys[i].ChildrenName).val(citys[i].ChildrenCode);
_city.append(option);
}
})
} 页面元素如下
  <select id="Province"></select>
<select id="City"></select>

jquery省市联动,根据公司需求而写的更多相关文章

  1. jQuery省市联动(XML/JSON)

    准备: 导包 在src下导入c3p0-config.xml 导入JDBCUtil 创建数据库 新建js文件夹导入jQuery配置文件 NO01:创建city.jsp页面 <%@ page lan ...

  2. jQuery省市联动

    <!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...

  3. jquery插件-省市联动

        由于项目需要需要实现一个省市联动,由于业务有一些特殊的需求,使用现有的插件略有不便,就自己实现了一个.     首先需要保存地区数据的JS数据文件,我这里命名为areaData.js,内容如下 ...

  4. 用jquery实现省市联动

    <!-- 需求: [1] 动态生成省份选择框. [2] 当选择了省份的某一项时, 动态改变 城市选择中的列表项. --> <!DOCTYPE html> <html la ...

  5. Json 基于jQuery+JSON的省市联动效果

    helloweba.com 作者:月光光 时间:2012-09-12 21:57 标签: jQuery  JSON  Ajax  省市联动     省市区联动下拉效果在WEB中应用非常广泛,尤其在一些 ...

  6. jQuery实现省市联动

    未实现任何功能之前: 实现功能之后: 实现该功能的jQuery核心代码: <script> var provinceArr=new Array(5); provinceArr[0]=new ...

  7. Django(二十)下拉列表-省市联动实例:jquery的ajax处理前端

    一.知识点 1.jquery的ajax请求写法 <script src="/static/js/jquery-1.12.4.min.js"></script> ...

  8. Ajax省市联动

    以JQuery为JS,写的Ajax省市联动. 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&q ...

  9. html + ashx 实现Ajax省市联动

    基本思路:1.了解数据库中省和市的表结构及关联主键 2.创建html页面及select标签 3.通过ajax向ashx(一般处理程序)发送后台请求完成联动效果 表结构: 这里,开始创建一个命为demo ...

随机推荐

  1. LCM在Kernel中的代码分析

    lcm的分析首先是mtkfb.c 1.mtk_init中platform_driver_register(&mtkfb_driver)注册平台驱动 panelmaster_init(); DB ...

  2. UIScrollView内容缩放

    •有些时候,我们可能要对某些内容进行手势缩放,如下图所示 UIScrollView不仅能滚动显示大量内容,还能对其内容进行缩放处理 也就是说,要完成缩放功能的话,只需要将需要缩放的内容添加到UIScr ...

  3. Windows7 安装vs2015 之后 调试Web项目IIS启动不了 aspnetcore.dll未能加载

    安装windows企业版,整整折腾了两天了,一个本身家里网络环境不好,时不时掉线,终于披荆斩棘,克服了所有困难,结果VS2015 EnterPrise 版本在调试Web环境的时候,始终在任务栏里找不到 ...

  4. WebApi系列~基于RESTful标准的Web Api

    微软的web api是在vs2012上的mvc4项目绑定发行的,它提出的web api是完全基于RESTful标准的,完全不同于之前的(同是SOAP协议的)wcf和webService,它是简单,代码 ...

  5. C#枚举中的位运算权限分配浅谈

    常用的位运算主要有与(&), 或(|)和非(~), 比如: 1 & 0 = 0, 1 | 0 = 1, ~1 = 0 在设计权限时, 我们可以把权限管理操作转换为C#位运算来处理. 第 ...

  6. JavaScript 闭包系列二(匿名函数及函数的闭包)

    一. 匿名函数 1. 函数的定义,可分为三种 1) 函数声明方式 function double(x) {     return 2*x; } 2)Function构造函数,把参数列表和函数体都作为字 ...

  7. 【转】MySQL中varchar最大长度是多少?

    一. varchar存储规则: 4.0版本以下,varchar(20),指的是20字节,如果存放UTF8汉字时,只能存6个(每个汉字3字节) 5.0版本以上,varchar(20),指的是20字符,无 ...

  8. MicroERP更新记录2.2:增加B/S查询功能

    提供电脑版及手机版两种浏览方式,仅仅作为一个DEMO展示,由于系统内置报表比较复杂,待广泛收集用户需求之后再逐步完善. 网页文件在安装包内web文件夹中. 下载地址:60.2.39.130/micro ...

  9. json处理总结(前端js和后端java)

    前端(js): json是以文本,即字符串的形式传递的,而JS操作的是JSON对象,所以,JSON对象和JSON字符串之间的相互转换是关键,下面将介绍两者之间的相互转换. json字符串:var st ...

  10. Individual Project - Word frequency program

    1.项目预计用时 -计划学习C#和百度一些用法的时间:5小时 -项目本身打算写两个类,一个是遍历搜索文件夹的,另外一个用来统计单词.计划用时:5小时 2.项目实际用时 学习C#以及正则表达式的用法:3 ...