Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use JavaScript to realize simple interaction between provinces and cities

Shulou Source: shulou.com Published: 2022-06-03 07:10:42 09月13日 Update

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

Tags: Array province and city element country event method two-dimensional learning same content step China Japan United States United Kingdom drop-down selection two name idea Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei NVidia macOS Shulou Information Xiaomi