JavascriptIt is a kind of object-oriented dynamic client scripting language that is prototyped and inherited from Netscape LiveScript. It mainly aims to solve server-side languages, such as Perl, the speed issues left over to provide customers with smoother browsing results.
This is simple and basic.Linkage drop-down boxThis example is mainly for those with only two levels of association, and the first level is a fixed option, the second level content is relatively simple. Javascript is used to link the second-level drop-down box.
Display code printing
- <! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
- <HTML>
- <HEAD>
- <TITLE> New Document </TITLE>
- <Meta name = "Generator" CONTENT = "EditPlus">
- <Meta name = "Author" CONTENT = "">
- <Meta name = "Keywords" CONTENT = "">
- <Meta name = "Description" CONTENT = "">
- <Script language = "JavaScript" type = "text/javascript">
- // Defines the two-dimensional array of the city, in which the order is the same as that of the province. Obtain the province base value through selectedIndex to obtain the corresponding city array.
- Var city = [
- ["Beijing", "Tianjin", "Shanghai", "Chongqing"],
- ["Nanjing", "Suzhou", "Nantong", "Changzhou"],
- ["Fuzhou", "Fuan", "Longyan", "Nanping"],
- ["Guangzhou", "Chaoyang", "Chaozhou", "Chenghai"],
- ["Lanzhou", "Baiyin", "Dingxi", "Dunhuang"]
- ];
- Function getCity (){
- // Obtain the province drop-down list object
- Var sltProvince = document. form1.province;
- // Obtain the objects in the city drop-down list
- Var sltCity = document. form1.city;
- // Obtain the city array of the corresponding province
- Var provinceCity = city [sltProvince. selectedIndex-1];
- // Clear the city drop-down list. Only the prompt option is left.
- SltCity. length = 1;
- // Fill the value in the city array in the city drop-down list
- For (var I = 0; I <provinceCity. length; I ++ ){
- SltCity [I + 1] = new Option (provinceCity [I], provinceCity [I]);
- }
- }
- </Script>
- </HEAD>
- <BODY>
- <Form method = post action = "" name = "form1">
- <Select name = "province" onChange = "getCity ()">
- <Option value = "0"> select the province </OPTION>
- <Option value = "municipality"> municipality </OPTION>
- <Option value = "Jiangsu Province"> Jiangsu Province </OPTION>
- <Option value = "Fujian Province"> Fujian Province </OPTION>
- <Option value = "Guangdong Province"> Guangdong Province </OPTION>
- <Option value = "Gansu Province"> Gansu Province </OPTION>
- </SELECT>
- <Select name = "city">
- <Option value = "0"> select the city </OPTION>
- </SELECT>
- </FORM>
- </BODY>
- </HTML>
This code is relatively simple.
If you are not familiar with js, you can look at the following content about how js processes select objects:
1. Use the selectedIndex attribute to obtain the index of the current option
The option in the drop-down box is a linear array. Each option has an index. selectedIndex indicates the index number of the selected option. Combined with the options attribute, you can obtain the selected option object for further processing. When multiple options are available in the drop-down box, the selectedIndex attribute returns the first selected index.
SelectedIndex is a read-only attribute. To set the items in the drop-down box specified by the index to the selected state, you can set the option object's selected to true.
2. Add an option for the select object
Display code printing
- sltCity[i+1]=new Option(provinceCity[i],provinceCity[i]);
- new Option(provinceCity[i],provinceCity[i])
Creates an option object whose value is provinceCity [I] and whose text is provinceCity [I]. sltCity is the city object on the page, and I + 1 specifies the location of the new option.
3. Clear a select object
There are two ways to delete all the options in the drop-down box. The first method is to traverse and delete:
Display code printing
- var l=myselect.length;
- for(var i=0;i<l;i++){
- myselect.options[i]=null;
- }
-
The second method is relatively simple, so this method is generally used:
Display code printing
- myselect.length=0;
I hope this article will help you. We will continue to discuss other issues related to drop-down box interaction in subsequent chapters.