Ideas:
<body> Province: <select id= "Province" > <option value= "" > Please select </option><option value= " Hebei Province "> Hebei province </option><option value=" Guangxi "> Guangxi </option><option value=" Shandong province "> Shandong Province </option > </select> cities: <select id= "City" > <option value= "" > Please select </option> </ Select> </body>
1. First get the current label based on ID province.
2. The current selection value is obtained according to This.value. For example, Shandong.
3. Create an array that contains the cities under the province.
4. Traverse the province according to the currently selected province.
5. Create an Option node: Add to tag with ID city
6. Test found that each time the switch to clear all the children in the current province node
Code:
<script type= "Text/javascript" > document.getElementById ("province"). Onchange=function () {var Provinceelement=this.value;var City=document.getelementbyid ("City"); var options=city.getelementsbytagname (" Option "); for (Var i=options.length-1;i>0;i--) {city.removechild (options[i]);} var arry;if (provinceelement== "Hebei province") {array=["Xingtai", "Shijiazhuang", "Tangshan", "Handan", "Baoding"];} else if (provinceelement== "Guangxi") {array=["Guilin", "Nanning", "Liuzhou", "Yulin"];} else{array=["Jinan", "Qingdao", "Yantai", "Weihai";} for (Var i=0;i<array.length;i++) {var op=document.createelement ("option"); Op.setattribute ("Value", array[i]); var Text=document.createtextnode (Array[i]); Op.appendchild (text); City.appendchild (OP);}} </script>
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
JavaScript Two-level linkage