Copy codeThe Code is as follows:
// Company two-dimensional array data source
Office = [
["Dept", "ParentDept"],
["Business Department", "0"],
["Technology Department", "0"],
["Marketing Department", "0"],
["Www.yongfa365.com", "Business Department"],
["Business department Xiao Yang", "Business Department"],
["Business department dishes", "Business Department"],
["Old Liu, Technical Department"],
["Technical Department Lao Yang", "Technical Department"],
["Old dishes of Technical Department", "Technology Department"],
["Mr. Liu from marketing department", "Marketing Department"],
["Mr. Yang from the Marketing Department", "Marketing Department"],
["Cainiao in the marketing department", "Marketing Department"]
]
// Provincial and municipal 3D array data sources
City3 = [
["Dept", "ParentId", "Id"],
["Beijing", "0", "010"],
["Shanxi", "0", "0359"],
["Chaoyang District", "010", "001"],
["Haidian District", "010", "002"],
["Dougezhuang", "001", "101"],
["Shilibao", "001", "102"],
["Zhongguancun", "002.", "201"],
["Shangdi", "002", "202"],
["Yuncheng region", "0359", "301"],
["Taiyuan City", "0359", "302"],
["Yongji City", "301", "311"],
["Community", "302", "312"]
];
// FillSelectTree ("nowhaha", City3, "0", "311",-1)
// Select Control ID, data source, parent value, selected value, level (used to generate separators)
Function FillSelectTree (SelectId, ArrObj, ParentValue, SelectedValue, NowI ){
StrObj = eval (document. getElementById (SelectId ));
NowI ++;
// Determine whether the data source is a level-2 data source or level-3 data source.
If (ArrObj [0]. length = 2)
{ArrNum = 0 ;}
Else
{ArrNum = 2 ;}
// Generate all options
For (var I = 0; I <ArrObj. length; I ++ ){
If (ArrObj [I] [1] = ParentValue ){
StrObj. options [StrObj. length] = new Option (String ("", NowI) + ArrObj [I] [0], ArrObj [I] [ArrNum]);
// Select the default value
If (ArrObj [I] [ArrNum] = SelectedValue ){
StrObj. options [StrObj. length-1]. selected = true;
}
// Traverse
FillSelectTree (SelectId, ArrObj, ArrObj [I] [ArrNum], SelectedValue, NowI );
}
}
}
// How many times do I copy a string?
Function String (str, nowi ){
Strtemp = "";
For (var I = 0; I <nowi; I ++ ){
Strtemp + = str;
}
Return strtemp;
}
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <ptml xmlns = "http://www.w3.org/1999/xhtml"> <pead> <title> </title> </pead> <body> <pr/> <select id = "nowhaha"> </select> <select id = "NowOffice"> </select> </body> </ptml>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]