Javascript code for generating an infinite drop-down list

Source: Internet
Author: User

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]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.