1 //author:guan 2 //2015-05-25 3 //省市联动 4 //实用说明,页面引用如下js 5 //<script src="../js/jquery-1.6.3.min.js"></script> 6 //<script src="js/ProvinceDropDown.js"></script> 7 //页面元素 8 //<select id="Province"></select> 9 //<select id="City"></select> 10 11 $(function () { 12 //初始化下拉框值 13 InitDropDown("Province", "City"); 14 }) 15 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" }] }]; 16 17 function InitDropDown(province, city) { 18 var _province = $("#" + province); 19 //初始化清空,为其添加请选择默认选项 20 _province.empty().append($("<option>").text("请选择").val("-1")); 21 22 var _city = $("#" + city); 23 _city.empty().append($("<option>").text("请选择").val("-1")); 24 //循环读取数组的json一级下拉值 25 for (var i = 0; i < Dropdowndata.length; i++) { 26 var option = $("<option>").text(Dropdowndata[i].ParentName).val(Dropdowndata[i].ParentCode).attr("pos", i); 27 _province.append(option); 28 } 29 $(_province).change(function () { 30 _city.empty().append($("<option>").text("请选择").val("-1")); 31 //根据数组下标从数据取出json数据 32 var citys = Dropdowndata[$(this).find("option:selected").attr("pos")].ChildrenNodes; 33 for (var i = 0; i < citys.length; i++) { 34 var option = $("<option>").text(citys[i].ChildrenName).val(citys[i].ChildrenCode); 35 _city.append(option); 36 } 37 }) 38 }
页面元素如下
<select id="Province"></select> <select id="City"></select>