How to use JavaScript to realize simple interaction between provinces and cities
This article mainly explains "how to use JavaScript to achieve simple provincial and municipal interaction". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use JavaScript to achieve simple provincial and municipal interaction".
Steps
* create a page with two drop-down selection boxes
* there is an event in the first drop-down box: change event onchange event
-method add1 (this.value); indicates the value value in the currently changed option
* create a two-dimensional array to store data
* the first element in each array is the name of the country, followed by the city within the country
*
1. Traverse a two-dimensional array
2. The result is also an array (national correspondence)
3. Compare the first value in the array with the passed value
4. If the same, get the element after the first value
5. Get the select of city
6. Add the past (use) appendChild method
-create an option (three steps)
/ *
Because you have to add option to the city every time
The second time it is added, it will be appended.
* before each addition, check whether there is option in city, and if so, delete it.
, /
The code is as follows:
Linkage between provinces and cities