JavaScript implements provincial and municipal linkage

Source: Internet
Author: User

<! DOCTYPE html>
<meta charset= "UTF-8" >
<title></title>
<script>
To define a two-dimensional array:
var arr = new Array (4);
Arr[0] = new Array ("Harbin", "Qiqihar", "Daqing", "Jiamusi");
ARR[1] = new Array ("Changchun", "Jilin", "Siping", "Tonghua");
ARR[2] = new Array ("Shenyang City", "Jinzhou", "Dalian", "Tieling");
ARR[3] = new Array ("Zhengzhou", "Luoyang", "Anyang", "Nanyang");

/*
1. Determine the event: OnChange Event
2. Event triggering function
3. function to do something
1. Get which province is currently selected
2. Access to all cities in the province
3. Traverse all the cities
4. Dynamic Creation <option> Shenzhen </option>
5. Attach the created structure to the Cityselect
*/
function Selectpro (value) {
alert (value);
2. Access to all cities in the province
var cities = Arr[value];

var cityselect = document.getElementById ("city");

Console.log (cityselect.innerhtml);
Empty the original content
cityselect.innerhtml = "";
citySelect.options.length = 0;


3. Traverse all the cities
for (Var i=0;i<cities.length;i++) {
Get City text content
var cityname = cities[i];
Console.log (CityName);

4. Create option <option></option>
var opt1 = document.createelement ("option");
5. Create a city text node
var textnode = document.createTextNode (CityName);
6. <option> text Nodes </option>
Opt1.appendchild (Textnode);

7. Add the created structure to the
var cityselect = document.getElementById ("city");
Cityselect.appendchild (OPT1);
}
}
</script>
<body>
<form action= "#" method= "Get" >
<input type= "hidden" name= "id" value= "007"/>
Name: <input name= "username" value= "Xuduoduo"/><br>
Password: <input type= "password" name= "password" value= "123" ><br>
Gender: <input type= "Radio" name= "Sex" value= "1" checked= "checked" > Male
<input type= "Radio" name= "Sex" value= "0" > Female
<br>
Hobbies: <input type= "checkbox" name= "hobby" value= "eat" > Eat
<input type= "checkbox" name= "hobby" value= "Drink" checked= "checked" > Drink
<input type= "checkbox" name= "hobby" value= "Sleep" checked= "checked" > Sleep
<br>
Avatar: <input type= "file" name= "Photo" ><br>
Origin:
<select name= "Pro" onchange= "Selectpro (this.value)" >
<option >-Please select-</option>
<option value= "0" > Heilongjiang </option>
<option value= "1" > Jilin </option>
<option value= "2" > Liaoning </option>
<option value= "3" > Henan </option>
</select>
<select name= "City" id= "City" >
<option >-Please select-</option>

</select>
<br>
Self Introduction:
<textarea name= "Intr" cols= "+" rows= "4" >good!</textarea>
<br>
<input type= "Submit" value= "Save"/>
<input type= "Reset"/>
<input type= "button" value= "normal button"/>
</form>
</body>
<!--

-

JavaScript implements provincial and municipal 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.