js 级联下拉框的生成

最近做页面时,碰到js级联下拉框显示的问题。先用下拉框选择大类,小类选项由大类的选项决定自动生成。我的js 代码如下:
var classidList=[
{'100':'旅游度假','subList':{'100001':'旅游资讯','100002':'旅行预定','100003':'旅行线路','100004':'机票预定','100005':'酒店预订','100006':'旅行社','100007':'热门景点','100008':'交通地图','100009':'天气','100010':'旅游门户'}},
{'101':'医疗健康','subList':{'101001':'健康资讯','101002':'医疗','101003':'保健','101004':'两性健康','101005':'心理健康','101006':'医学','101007':'医药','101008':'医院','101009':'健康门户'}},
{'102':'教育招聘','subList':{'102001':'教育资讯','102002':'人才招聘','102003':'论文','102004':'课件','102005':'学习/培训','102006':'留学','102007':'外语','102008':'同学录','102009':'大学','102010':'网校','102011':'考试','102012':'教育门户'}},{'103':'消费购物','subList':{'103001':'消费资讯','103002':'购物','103003':'美食','103004':'时尚','103005':'摄影','103006':'美容','103007':'鞋帽','103008':'服装服饰','103009':'男士','103010':'女性','103011':'育儿','103012':'行业网站'}},
{'104':'网络服务','subList':{'104001':'搜索','104002':'垂直搜索','104003':'P2P','104004':'软件下载','104005':'在线翻译','104006':'网站导航','104007':'免费主页','104008':'桌面媒体','104009':'网络硬盘','104010':'网络电话'}},
{'105':'社区交友','subList':{'105001':'聊天','105002':'论坛','105003':'播客','105004':'社区','105005':'博客','105006':'综合交友','105007':'婚恋交友','105008':'商务交友','105009':'维客'}},
{'106':'娱乐休闲','subList':{'106001':'游戏','106002':'影视','106003':'音乐','106004':'动漫','106005':'明星','106006':'宠物','106007':'贺卡','106008':'视频','106009':'爱情','106010':'图片','106011':'星座','106012':'笑话幽默','106013':'娱乐门户'}},
{'107':'新闻门户','subList':{'107001':'综合门户','107002':'新闻门户','107003':'地方信息港','107004':'政府公务','107005':'电视','107006':'广播电台','107007':'报刊'}},
{'108':'IT数码','subList':{'108001':'IT资讯','108002':'电脑教程','108003':'硬件','108004':'软件','108005':'电脑考试','108006':'数码','108007':'程序设计','108008':'笔记本','108009':'LINUX','108010':'站长资源','108011':'IT门户'}},
{'109':'体育运动','subList':{'109001':'奥运','109002':'足球','109003':'篮球','109004':'排球','109005':'乒乓球','109006':'网球','109007':'体育报刊','109008':'棋牌','109009':'游泳','109010':'羽毛球','109011':'户外','109012':'赛车F1','109013':'体育门户'}},
{'110':'文化艺术','subList':{'110001':'文学','110002':'艺术','110003':'历史','110004':'军事','110005':'社科','110006':'名人'}},
{'111':'财经法律','subList':{'111001':'证券','111002':'彩票','111003':'银行','111004':'汽车','111005':'房产','111006':'保险','111007':'商业','111008':'理财','111009':'广告','111010':'会计','111011':'法律','111012':'财经门户'}}
];

//设置小类所对应的Map值;
function setSubClass(province)
{	var subClassMap;
	for(i=0;i<classidList.length;i++){
		var classMap = classidList[i];
		for(var prop in classMap){
			if(prop==province){
			subClassMap=classMap['subList'];
			}
		}
	}
	setSelectOption('site_sub_class', subClassMap, '-请选择小类-');
}
//根据大类的改变,相应小类进行初始化。
function setSelectOption(selectObj, optionList, firstOption, selected)
{
	if (typeof selectObj != 'object')
	{
		selectObj = document.getElementById(selectObj);
	}
	removeOptions(selectObj);
	var start = 0;
	if (firstOption)
	{
		selectObj.options[0] = new Option(firstOption, '');
		start ++;
	}
	for(var prop in optionList){
		selectObj.options[start] = new Option(optionList[prop], prop);
		if(selected == prop)
		{
			selectObj.options[start].selected = true;
		}
		start ++;
	}
}
//清除子类选项
function removeOptions(selectObj)
{
	if (typeof selectObj != 'object')
	{
		selectObj = document.getElementById(selectObj);
	}
	var len = selectObj.options.length;
	for (var i=0; i < len; i++)
	{
		selectObj.options[0] = null;
	}
}

//初始化
var mainClassid = document.getElementById('site_main_class');
//读取大类并添加到大类选择中
for(iter=0;iter<classidList.length;iter++)
{
	var classMap = classidList[iter];
	for(var prop in classMap){
		if(prop!='subList'){
		mainClassid.options.add(new Option(classMap[prop],prop));
		}
	}
}


在解决这个问题时主要是怎么组织这个数据结构,我是利用了js 的Map 来进行迭代。从而解决了这个问题的。

你可能感兴趣的:(医疗,招聘,资讯,体育,旅游)