By traversing a two-dimensional array to get to the level two list of each option
Then the onchange event gets to the province, and then loops through the province's city and adds the traversed city to the selector with the ID cities. Get the content you need to empty the level two list, or you won't be able to refresh the next selection of the provincial Two cities list
<script>//1. Create a two-dimensional array for storing provinces and cities varcities=NewArray (3); cities[0]=NewArray ("Wuhan", "Huanggang", "Xiangyang City", "Jinzhou"); cities[1]=NewArray ("Changsha", "Chenzhou", "Zhuzhou", "Yueyang"); cities[2]=NewArray ("Shijiazhuang City", "Hanhan", "Langfang", "Baoding"); cities[3]=NewArray ("Zhengzhou", "Luoyang", "Kaifeng", "Anyang"); functionChangecity (val) {//7. Get a second drop-down list varCityele=document.getelementbyid ("City"); //9. Empty the option content of the second drop-down listCityele.options.length=0; //2. Traversing the provinces in a two-dimensional array for(vari=0;i<cities.length;i++){ //The index of the province is compared if(val==i) { //3. Traverse the city under the user's chosen province for(varj=0;j<cities.length;j++){ //alert (cities[i][j]); //4. Create a text node for the city varTextnode=document.createTextNode (Cities[i][j]); //5. Create an OPTION element node varOpele=document.createelement ("option"); //6. Add a city's text node to the OPTION element nodeOpele.appendchild (Textnode); //8. Add an OPTION element node to the second drop-down listCityele.appendchild (Opele); } } } } </script>
<select onchange= "changecity (this.value)" > <option>--Please select--</option> <option value= " 0 "> Hubei </option> <option value=" 1 "> Hunan </option> <option value=" 2 "> Hebei </option > <option value= "3" > Henan </option> </select> <select id= "City" > </ Select>
JavaScript provincial level Two linkage