JavaScript Two-level linkage

Source: Internet
Author: User

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

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.