JavaScript provincial level Two linkage

Source: Internet
Author: User

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

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.