欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

Javascript 生成无限下拉列表实现代码_javascript技巧

程序员文章站 2022-05-05 12:17:51
...
复制代码 代码如下:

//公司二维数组数据源
Office = [
["Dept", "ParentDept"],
["业务部", "0"],
["技术部", "0"],
["市场部", "0"],
["www.yongfa365.com", "业务部"],
["业务部小杨", "业务部"],
["业务部小菜", "业务部"],
["技术部老柳", "技术部"],
["技术部老杨", "技术部"],
["技术部老菜", "技术部"],
["市场部柳先生", "市场部"],
["市场部杨先生", "市场部"],
["市场部菜鸟", "市场部"]
]

//省市三维数组数据源
City3 = [
["Dept", "ParentId", "Id"],
["北京", "0", "010"],
["山西", "0", "0359"],
["朝阳区", "010", "001"],
["海淀区", "010", "002"],
["豆各庄", "001", "101"],
["十里堡", "001", "102"],
["中关村", "002", "201"],
["上地", "002", "202"],
["运城地区", "0359", "301"],
["太原市", "0359", "302"],
["永济市", "301", "311"],
["小区", "302", "312"]
];

//FillSelectTree("nowhaha", City3, "0", "311", -1)
//Select控件ID,数据源,父级值,选中值,第几级(用来产生分隔符)
function FillSelectTree(SelectId, ArrObj, ParentValue, SelectedValue, NowI) {
StrObj = eval(document.getElementById(SelectId));

NowI++;

//判断它是二级数据源,还是三级
if (ArrObj[0].length == 2)
{ ArrNum = 0; }
else
{ ArrNum = 2; }

//生成所有Option
for (var i = 0; i if (ArrObj[i][1] == ParentValue) {
StrObj.options[StrObj.length] = new Option(String("  ", NowI) + ArrObj[i][0], ArrObj[i][ArrNum]);

//选中默认值
if (ArrObj[i][ArrNum] == SelectedValue) {
StrObj.options[StrObj.length - 1].selected = true;
}

//遍历
FillSelectTree(SelectId, ArrObj, ArrObj[i][ArrNum], SelectedValue, NowI);
}
}
}


//复制字符串多少次
function String(str, nowi) {
strtemp = "";
for (var i = 0; i strtemp += str;
}
return strtemp;
}


[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]