Author: ybcola
This js code was written a long time ago, probably a year ago! The problem I encountered at that time was when I was building a rural talent management system, because the system required selection and adding provinces, cities, counties, and even townships and villages through the background, and when modifying talent information, it required using a drop-down list to select. , and the source of each talent may be a province or city, or a province, city, or county, or you have to choose when you choose a team! At that time, my first reaction was to find out if there was any ready-made code on the Internet. The most commonly used one I found was provincial and municipal level two linkage. At that time, that js code could be seen everywhere on the Internet, but it was useless to me because I needed it. It is a code that is multi-level and can be expanded freely! In the end, I wrote the JS code myself.
In order to consider using it in multiple places on the same page, they were designed using the idea of objects. In order to realize the different contents of each multi-level linkage, the internal data structure was separated from the program, so Design the final calling method such as:
var city=new LightManAddressTree;
city.selectshow(obj,0); //obj is the dom node to be added,
if any The html code is as follows
Then the calling method should be
obj=document.getElementById("abc");
var city=new LightManAddressTree;
city.selectshow(obj,0);
In this way, the multi-level linkage content is displayed at the position within the closed div tag
The js design code is as follows:
//Start defining address data
var AddressTree={};
AddressTree["a_row"]=new Array(); //存所有记录
with(AddressTree){
a_row["1"]=["北京市",0,1];
a_row["2"]=["上海市",0,1];
a_row["3"]=["天津市",0,1];
a_row["4"]=["重庆市",0,1];
a_row["5"]=["河北",0,0];
a_row["6"]=["山西",0,0];
a_row["7"]=["内蒙古",0,0];
a_row["8"]=["辽宁",0,0];
a_row["9"]=["吉林",0,0];
a_row["10"]=["黑龙江",0,0];
a_row["11"]=["江苏",0,0];
a_row["12"]=["渐江",0,0];
a_row["13"]=["安徽",0,0];
a_row["14"]=["福建",0,0];
a_row["15"]=["江西",0,0];
a_row["16"]=["山东",0,0];
a_row["17"]=["河南",0,0];
a_row["18"]=["湖北",0,0];
a_row["19"]=["湖南",0,0];
a_row["20"]=["广东",0,0];
a_row["21"]=["广西",0,0];
a_row["22"]=["海南",0,0];
a_row["23"]=["四川",0,0];
a_row["24"]=["贵州",0,0];
a_row["25"]=["云南",0,0];
a_row["26"]=["西藏",0,0];
a_row["27"]=["陕西",0,0];
a_row["28"]=["甘肃",0,0];
a_row["29"]=["青海",0,0];
a_row["30"]=["宁夏",0,0];
a_row["31"]=["新壃",0,0];
a_row["32"]=["香港",0,1];
a_row["33"]=["澳门",0,1];
a_row["34"]=["台湾",0,0];
a_row["35"]=["石家庄",5,1];
a_row["36"]=["唐山",5,1];
a_row["37"]=["秦皇岛",5,1];
a_row["38"]=["邯郸",5,1];
a_row["39"]=["邢台",5,1];
a_row["40"]=["保定",5,1];
a_row["41"]=["张家口",5,1];
a_row["42"]=["承德",5,1];
a_row["43"]=["沧州",5,1];
a_row["44"]=["廊坊",5,1];
a_row["45"]=["衡水",5,1];
a_row["46"]=["太原",6,1];
a_row["47"]=["大同",6,1];
a_row["48"]=["阳泉",6,1];
a_row["49"]=["长治",6,1];
a_row["50"]=["晋城",6,1];
a_row["51"]=["朔州",6,1];
a_row["52"]=["晋中",6,1];
a_row["53"]=["运城",6,1];
a_row["54"]=["忻州",6,1];
a_row["55"]=["临汾",6,1];
a_row["56"]=["吕梁",6,1];
a_row["58"]=["呼和浩特",7,1];
a_row["59"]=["包头",7,1];
a_row["60"]=["乌海",7,1];
a_row["61"]=["赤峰",7,1];
a_row["62"]=["通辽",7,1];
a_row["63"]=["鄂尔多斯",7,1];
a_row["64"]=["呼伦贝尔",7,1];
a_row["65"]=["乌兰察布市",7,1];
a_row["66"]=["锡林郭勒盟",7,1];
a_row["67"]=["巴彦淖尔盟",7,1];
a_row["68"]=["阿拉善盟",7,1];
a_row["69"]=["兴安盟",7,1];
a_row["70"]=["沈阳",8,1];
a_row["71"]=["大连",8,1];
a_row["72"]=["鞍山",8,1];
a_row["73"]=["抚顺",8,1];
a_row["74"]=["本溪",8,1];
a_row["75"]=["丹东",8,1];
a_row["76"]=["锦州",8,1];
a_row["77"]=["葫芦岛",8,1];
a_row["78"]=["营口",8,1];
a_row["79"]=["盘锦",8,1];
a_row["80"]=["阜新",8,1];
a_row["81"]=["辽阳",8,1];
a_row["82"]=["铁岭",8,1];
a_row["83"]=["朝阳",8,1];
a_row["84"]=["长春",9,1];
a_row["85"]=["吉林",9,1];
a_row["86"]=["四平",9,1];
a_row["87"]=["辽源",9,1];
a_row["88"]=["通化",9,1];
a_row["89"]=["白山",9,1];
a_row["90"]=["松原",9,1];
a_row["91"]=["白城",9,1];
a_row["92"]=["延办朝鲜族自治州",9,1];
a_row["93"]=["哈尔滨",10,1];
a_row["94"]=["齐齐哈尔",10,1];
a_row["95"]=["鹤岗",10,1];
a_row["96"]=["双鸭山",10,1];
a_row["97"]=["鸡西",10,1];
a_row["98"]=["大庆",10,1];
a_row["99"]=["伊春",10,1];
a_row["100"]=["牡丹江",10,1];
a_row["101"]=["佳木斯",10,1];
a_row["102"]=["七台河",10,1];
a_row["103"]=["黑河",10,1];
a_row["104"]=["绥化",10,1];
a_row["105"]=["大兴安岭",10,1];
a_row["106"]=["南京",11,1];
a_row["107"]=["无锡",11,1];
a_row["108"]=["徐州",11,1];
a_row["109"]=["常州",11,1];
a_row["110"]=["苏州",11,1];
a_row["111"]=["南通",11,1];
a_row["112"]=["连云港",11,1];
a_row["113"]=["淮安",11,1];
a_row["114"]=["盐城",11,1];
a_row["115"]=["扬州",11,1];
a_row["116"]=["镇江",11,1];
a_row["117"]=["泰州",11,1];
a_row["118"]=["宿迀",11,1];
a_row["119"]=["杭州",12,1];
a_row["120"]=["宁波",12,1];
a_row["121"]=["温州",12,1];
a_row["122"]=["嘉兴",12,1];
a_row["123"]=["湖州",12,1];
a_row["124"]=["绍兴",12,1];
a_row["125"]=["金华",12,1];
a_row["126"]=["舟山",12,1];
a_row["127"]=["台州",12,1];
a_row["128"]=["丽水",12,1];
a_row["129"]=["衢州",12,1];
a_row["130"]=["合肥",13,1];
a_row["131"]=["芜湖",13,1];
a_row["132"]=["蚌埠",13,1];
a_row["133"]=["淮南",13,1];
a_row["134"]=["马鞍山",13,1];
a_row["135"]=["淮北",13,1];
a_row["136"]=["铜陵",13,1];
a_row["137"]=["安庆",13,1];
a_row["138"]=["黄山",13,1];
a_row["139"]=["滁州",13,1];
a_row["140"]=["阜阳",13,1];
a_row["141"]=["宿州",13,1];
a_row["142"]=["巢湖",13,1];
a_row["143"]=["六安",13,1];
a_row["144"]=["毫州",13,1];
a_row["145"]=["池州",13,1];
a_row["146"]=["宣城",13,1];
a_row["147"]=["福州",14,1];
a_row["148"]=["厦门",14,1];
a_row["149"]=["莆田",14,1];
a_row["150"]=["三明",14,1];
a_row["151"]=["泉州",14,1];
a_row["152"]=["漳州",14,1];
a_row["153"]=["南平",14,1];
a_row["154"]=["龙岩",14,1];
a_row["155"]=["宁德",14,1];
a_row["156"]=["南昌",15,1];
a_row["157"]=["景德镇",15,1];
a_row["158"]=["萍乡",15,1];
a_row["159"]=["新余",15,1];
a_row["160"]=["九江",15,1];
a_row["161"]=["鹰潭",15,1];
a_row["162"]=["吉安",15,1];
a_row["163"]=["宜春",15,1];
a_row["164"]=["抚州",15,1];
a_row["165"]=["上饶",15,1];
a_row["166"]=["赣州",15,1];
a_row["167"]=["济南",16,1];
a_row["168"]=["青岛",16,1];
a_row["169"]=["淄博",16,1];
a_row["170"]=["枣庄",16,1];
a_row["171"]=["东营",16,1];
a_row["172"]=["潍坊",16,1];
a_row["173"]=["烟台",16,1];
a_row["174"]=["威海",16,1];
a_row["175"]=["济宁",16,1];
a_row["176"]=["泰安",16,1];
a_row["177"]=["日照",16,1];
a_row["178"]=["莱芜",16,1];
a_row["179"]=["德州",16,1];
a_row["180"]=["临沂",16,1];
a_row["181"]=["聊城",16,1];
a_row["182"]=["滨州",16,1];
a_row["183"]=["菏泽",16,1];
a_row["184"]=["郑州",17,1];
a_row["185"]=["开封",17,1];
a_row["186"]=["洛阳",17,1];
a_row["187"]=["平顶山",17,1];
a_row["188"]=["焦作",17,1];
a_row["189"]=["鹤壁",17,1];
a_row["190"]=["新乡",17,1];
a_row["191"]=["安阳",17,1];
a_row["192"]=["濮阳",17,1];
a_row["193"]=["许昌",17,1];
a_row["194"]=["漯河",17,1];
a_row["195"]=["三门峡",17,1];
a_row["196"]=["南阳",17,1];
a_row["197"]=["南丘",17,1];
a_row["198"]=["信阳",17,1];
a_row["199"]=["周口",17,1];
a_row["200"]=["驻马店",17,1];
a_row["201"]=["济源",17,1];
a_row["202"]=["武汉",18,1];
a_row["203"]=["黄石",18,1];
a_row["204"]=["襄樊",18,1];
a_row["205"]=["十堰",18,1];
a_row["206"]=["荆州",18,1];
a_row["207"]=["宜昌",18,1];
a_row["208"]=["荆门",18,1];
a_row["209"]=["鄂州",18,1];
a_row["210"]=["孝感",18,1];
a_row["211"]=["黄冈",18,1];
a_row["212"]=["咸宁",18,1];
a_row["213"]=["随州",18,1];
a_row["214"]=["仙桃",18,1];
a_row["215"]=["天门",18,1];
a_row["216"]=["潜江",18,1];
a_row["217"]=["神农架",18,1];
a_row["218"]=["恩施土家族自治州",18,1];
a_row["219"]=["长沙",19,1];
a_row["220"]=["株洲",19,1];
a_row["221"]=["湘潭",19,1];
a_row["222"]=["衡阳",19,1];
a_row["223"]=["邵阳",19,1];
a_row["224"]=["岳阳",19,1];
a_row["225"]=["常德",19,1];
a_row["226"]=["张家界",19,1];
a_row["227"]=["益阳",19,1];
a_row["228"]=["郴州",19,1];
a_row["229"]=["怀化",19,1];
a_row["230"]=["娄底",19,1];
a_row["231"]=["湘西土家族苗族自治州",19,1];
a_row["232"]=["广州",20,1];
a_row["233"]=["深圳",20,1];
a_row["234"]=["珠海",20,1];
a_row["235"]=["汕头",20,1];
a_row["236"]=["韶关",20,1];
a_row["237"]=["佛山",20,1];
a_row["238"]=["江门",20,1];
a_row["239"]=["湛江",20,1];
a_row["240"]=["茂名",20,1];
a_row["241"]=["肇庆",20,1];
a_row["242"]=["惠州",20,1];
a_row["243"]=["梅州",20,1];
a_row["244"]=["汕尾",20,1];
a_row["245"]=["河源",20,1];
a_row["246"]=["阳江",20,1];
a_row["247"]=["清远",20,1];
a_row["248"]=["东莞",20,1];
a_row["249"]=["中山",20,1];
a_row["250"]=["潮州",20,1];
a_row["251"]=["揭阳",20,1];
a_row["252"]=["云浮",20,1];
a_row["253"]=["南宁",21,1];
a_row["254"]=["柳州",21,1];
a_row["255"]=["桂林",21,1];
a_row["256"]=["梧州",21,1];
a_row["257"]=["北海",21,1];
a_row["258"]=["防城港",21,1];
a_row["259"]=["钦州",21,1];
a_row["260"]=["贵港",21,1];
a_row["261"]=["玉林",21,1];
a_row["262"]=["百色",21,1];
a_row["263"]=["贺州",21,1];
a_row["264"]=["河池",21,1];
a_row["265"]=["来宾",21,1];
a_row["266"]=["崇左",21,1];
a_row["267"]=["海口",22,1];
a_row["268"]=["三亚",22,1];
a_row["269"]=["五指山",22,1];
a_row["270"]=["琼海",22,1];
a_row["271"]=["儋州",22,1];
a_row["272"]=["文昌",22,1];
a_row["273"]=["方宁",22,1];
a_row["274"]=["东方",22,1];
a_row["275"]=["澄迈",22,1];
a_row["276"]=["定安",22,1];
a_row["277"]=["屯昌",22,1];
a_row["278"]=["临高",22,1];
a_row["279"]=["白沙黎族自治县",22,1];
a_row["280"]=["江黎族自治县",22,1];
a_row["281"]=["乐东黎族自治县",22,1];
a_row["282"]=["陵水黎族自治县",22,1];
a_row["283"]=["保亭黎族苗族自治县",22,1];
a_row["284"]=["琼中称许苗族自治县",22,1];
a_row["285"]=["成都",23,1];
a_row["286"]=["自贡",23,1];
a_row["287"]=["攀枝花",23,1];
a_row["288"]=["泸州",23,1];
a_row["289"]=["德阳",23,1];
a_row["290"]=["Mianyang",23,1];
a_row["291"]=["Guangyuan",23,1];
a_row["292"]=["Suining ",23,1];
a_row["293"]=["Neijiang",23,1];
a_row["294"]=["Leshan",23,1];
a_row["295"]=["Nanchong",23,1];
a_row["296"]=["Yibin",23,1];
a_row["297"]=["Guang'an ",23,1];
a_row["298"]=["Dazhou",23,1];
a_row["299"]=["Meishan",23,1];
a_row["300"]=["Ya'an",23,1];
a_row["301"]=["Bazhong",23,1];
a_row["302"]=[" Ziyang",23,1];
a_row["303"]=["Aba Tibetan and Qiang Autonomous Prefecture",23,1];
a_row["304"]=["Ganzi Tibetan Autonomous Prefecture",23, 1];
a_row["305"]=["Liangshan Yi Autonomous Prefecture",23,1];
a_row["306"]=["Guiyang",24,1];
a_row[ "307"]=["Liupanshui",24,1];
a_row["308"]=["Zunyi",24,1];
a_row["309"]=["Anshun", 24,1];
a_row["310"]=["Tongren",24,1];
a_row["311"]=["Bijie",24,1];
a_row[ "312"]=["Qiandongnan Buyi and Miao Autonomous Prefecture",24,1];
a_row["313"]=["Qiandongnan Miao Autonomous Prefecture",24,1];
a_row["314 "]=["Qiannan Buyi and Miao Autonomous Prefecture",24,1];
a_row["315"]=["Kunming",25,1];
a_row["316"]=[" Qujing",25,1];
a_row["317"]=["Yuxi",25,1];
a_row["318"]=["Baoshan",25,1];
a_row["319"]=["Zhaotong",25,1];
a_row["320"]=["Lijiang",25,1];
a_row["321"]=[" Simao",25,1];
a_row["322"]=["Jiancang",25,1];
a_row["323"]=["Wenshan Zhuang and Miao Autonomous Prefecture",25,1 ];
a_row["324"]=["Honghe Hani and Yi Autonomous Prefecture",25,1];
a_row["325"]=["Xishuangbanna Dai Autonomous Prefecture",25,1];
a_row["326"]=["Chuxiong Yi Autonomous Prefecture",25,1];
a_row["327"]=["Dali Bai Autonomous Prefecture",25,1];
a_row["328" ]=["Dehong Dai and Jingpo Autonomous Prefecture",25,1];
a_row["329"]=["Nujiang Lisu Autonomous Prefecture",25,1];
a_row["330"] =["Diqing Tibetan Autonomous Prefecture",25,1];
a_row["331"]=["Lhasa",26,1];
a_row["332"]=["Nagqu",26 ,1];
a_row["333"]=["Chamdo",26,1];
a_row["334"]=["Shannan",26,1];
a_row[" 335"]=["Shigatse",26,1];
a_row["336"]=["Ali",26,1];
a_row["337"]=["Lingzhi",26 ,1];
a_row["338"]=["Xi'an",27,1];
a_row["339"]=["Tongchuan",27,1];
a_row[" 340"]=["Baoji",27,1];
a_row["341"]=["Xianyang",27,1];
a_row["342"]=["Weinan",27 ,1];
a_row["343"]=["Yan'an",27,1];
a_row["344"]=["Hanzhong",27,1];
a_row[" 345"]=["Yulin",27,1];
a_row["346"]=["Ankang",27,1];
a_row["347"]=["Shangluo",27 ,1];
a_row["348"]=["Lanzhou",28,1];
a_row["349"]=["Jinchang",28,1];
a_row[" 350"]=["silver",28,1];
a_row["351"]=["Tianshui",28,1];
a_row["352"]=["Jiayuguan",28 ,1];
a_row["353"]=["Wuwei",28,1];
a_row["354"]=["Zhangye",28,1];
a_row[" 355"]=["Pingliang",28,1];
a_row["356"]=["Saquan",28,1];
a_row["357"]=["Qingyang" ,28,1];
a_row["358"]=["Dingxi",28,1];
a_row["359"]=["Longnan",28,1];
a_row ["360"]=["Linxia Hui Autonomous Prefecture",28,1];
a_row["361"]=["Gannan Tibetan Autonomous Prefecture",28,1];
a_row["362"]= ["Xining",29,1];
a_row["363"]=["Haidong",29,1];
a_row["364"]=["Haibei Tibetan Autonomous Prefecture",29, 1];
a_row["365"]=["Huangnan Tibetan Autonomous Prefecture",29,1];
a_row["366"]=["Hainan Tibetan Autonomous Prefecture",29,1];
a_row["367"]=["Goluo Tibetan Autonomous Prefecture",29,1];
a_row["368"]=["Yushu Tibetan Autonomous Prefecture",29,1];
a_row["369 "]=["Hai Mongolian Tibetan Autonomous Prefecture",29,1];
a_row["370"]=["Yinchuan",30,1];
a_row["371"]=["Shizuishan ",30,1];
a_row["372"]=["Wuzhong",30,1];
a_row["373"]=["Guyuan",30,1];
a_row["374"]=["Urumqi",31,1];
a_row["375"]=["Karamay",31,1];
a_row["376"]=["Shihezi ",31,1];
a_row["377"]=["Alar",31,1];
a_row["378"]=["Tumushuk",31,1] ;
a_row["379"]=["Wujiaqu",31,1];
a_row["380"]=["Tulu",31,1];
a_row[" 381"]=["Hami",31,1];
a_row["382"]=["Hotian",31,1];
a_row["383"]=["Aksu",31 ,1];
a_row["384"]=["Kashgar",31,1];
a_row["385"]=["Kizilsu Kirgiz Autonomous Prefecture",31,1];
a_row["386"]=["Bayingoleng Mongolian Autonomous Prefecture",31,1];
a_row["387"]=["Changji Hui Autonomous Prefecture",31,1];
a_row ["388"]=["Bortala Mongolian Autonomous Prefecture",31,1];
a_row["389"]=["Ili Kazakh Autonomous Prefecture",31,1];
a_row["390 "]=["Taipei",34,1];
a_row["391"]=["Kaohsiung",34,1];
a_row["392"]=["Keelung",34, 1];
a_row["393"]=["Taichung",34,1];
a_row["394"]=["Tainan",34,1];
a_row["395 "]=["Hsinchu",34,1];
a_row["396"]=["Chiayi",34,1];
a_row["397"]=["sss",285, 1];
}
var j=0;
AddressTree["a_record"]=new Array();//Storage record number
for(i in AddressTree["a_row"] ){AddressTree["a_record"][j ]=i;}
AddressTree.ObjNumber=1;
//Address selection constructor
function LightManAddressTree(){
var Data = AddressTree; //Data record data variable
this.noselectshow=["- -Please select a province--","--Please select a city--","--Please select a district or county--"];//Do not select display values at each level
this.grade=0; //Grade Number
this.idvalue=0; //The id number of the address selection value
this.text=""; //Address string
this.listseparator=" "; //In the address string Delimiter symbol
this.selectName="a_tree" (AddressTree.ObjNumber ) "_";
//End of defining address data
this.getfontpath = function(id){ //Get the superior of the specified record number Path
var path = "",upid;
try{
upid=(isNaN(id)?0:id);
while(upid!=0){
var upid= Data["a_row"][upid][1];
If(path!="")path=upid "," path;else path=upid;
}
}
catch( e){};
return eval("[" path "]");
}
//Create the following list
this.selectshowlevel=function(obj,upid,def){
var selectobj,optionobj,havedef;
selectobj=document.getElementById(this.selectName this.grade);
if(selectobj){
document.getElementById(this.selectName this.grade).options. length=0;
}else{
selectobj=document.createElement("select");
selectobj.id=this.selectName this.grade;
selectobj.grade=this.grade;
selectobj.obj=this;
selectobj.onchange=function(){
if(this.options[this.selectedIndex].value!=""){
this.obj.selectshow(obj ,this[this.selectedIndex].value); .obj.grade-1;i ){
var df=document.getElementById(this.obj.selectName i);
df.options.length=0;
defaultoption(this.obj,df, i);
🎜> }
defaultoption(this,selectobj, this.grade);
//Display data at level
if(upid>=0){
for(var j=0;j thisrecord=Data["a_row"][Data["a_record"][j]];
if(thisrecord[1]==upid){
optionobj=document.createElement("option");
optionobj.text=thisrecord[0];
optionobj.value=Data["a_record"][j];
selectobj.options.add(optionobj);
if(def==Data ["a_record"][j]) havedef=true;
}
}
}
if(havedef)selectobj.value=def;
return selectobj;
}
this.selectshow = function(obj,id){ //显示选中记录号
var selectobj,optionobj,thisrecord;
var lastgrade,tempid=id,tempgrade;
var fontpath=this.getfontpath(id);
tempgrade=this.grade;
this.grade=0;
//显示选中前部分内容
for (var i=0;i if(!document.getElementById(this.selectName this.grade)){
if(i this.Add(obj,this.selectshowlevel(obj,fontpath[i],fontpath[i 1]));
else
this.Add(obj,this.selectshowlevel(obj,fontpath[i],id));
}
this.grade ;
}
//显示选中后部分内容
do{
lastgrade=this.selectshowlevel(obj,tempid);
tempid=lastgrade.value;
if (tempid=="")tempid=-1;
this.Add(obj,lastgrade);
this.grade ;
}
while(lastgrade.options.length>0);
//删除多余选项
for(i=this.grade-1;i obj.removeChild(document.getElementById(this.selectName i));
}
//最得值
getvalue(this);
}
this.Add=function(obj,s){
if(s.options.length>0&&!document.getElementById(s.id))obj.appendChild(s);
}
//获取选择值
function getvalue(obj){
var sel
obj.text="";
obj.idvalue=0;
for(var i=0;i sel=document.getElementById(obj.selectName i);
if(sel.value!=""){
obj.text =(obj.text.match("\S")==null?"":obj.listseparator) sel.options[sel.selectedIndex].text;
obj.idvalue=sel.options[sel.selectedIndex].value;
}else{break;}
}
}
//级别显示数据
function defaultoption(obj,sobj,g){
try{
if(obj.noselectshow[g]){
optionobj=document.createElement("option");
optionobj.text=obj.noselectshow[g];
optionobj.value="";
sobj.options.add(optionobj,0);
}
}
catch(e){}
}
}
address_test.htm
"http://www.w3.org/TR/html4/loose.dtd">
Untitled Document