Realizing provincial and municipal linkage effects based on JavaScript

Realizing provincial and municipal linkage effects based on JavaScript

This article shares the specific code of JavaScript to achieve the linkage effect between provinces and cities for your reference. The specific content is as follows

Code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Provincial and municipal linkage effect</title>
</head>
<body onload="initProvince()">
Province: <select id="province" onchange="fillCity()"></select>
City: <select id="city"></select>
<script>
    /**
     * Initialize province function */
    function initProvince() {
        //Declare an array to store provinces let provinceArr=["Shaanxi Province","Sichuan Province","Henan Province","Shandong Province"];
        //Dynamically write the province array into the drop-down list //Get the province list object by id let proovinceObj=document.getElementById("province");
        //Set the content to be displayed when no selection is made let option=new Option("---Please select a province---","");
        proovinceObj.options.add(option);
        //Loop through the province array for (let province of provinceArr) {
            //Create Option object //Parameter 1: List display content //Parameter 2: Option's values ​​attribute value let option = new Option(province,province);
            //Add the option object to the provinceObj object proovinceObj.options.add(option);
        }
    }
    //Create a city array //Declare an array for storing cities let cityArr = new Array();
    cityArr['Shaanxi Province']=['Xi'an', 'Xianyang', 'Baoji', 'Hanzhong', 'Yan'];
    cityArr['Sichuan Province']=['Chengdu', 'Dazhou', 'Guangyuan', 'Mianyang', 'Leshan'];
    cityArr['Henan Province']=['Zhengzhou City', 'Kaifeng City', 'Luoyang City', 'Xinxiang City', 'Jiaozuo City'];
    cityArr['Shandong Province']=['Jinan City', 'Qingdao City', 'Laizhou City', 'Yantai City', 'Dezhou City'];

    /**
     * Fill cities according to provinces*/
    function fillCity() {
        //Get the currently selected province let provinceObj = document.getElementById("province");
        let province = provinceObj.value;
        //Get the city list object let cityObj = document.getElementById("city");
        // Clear the original data in the city list cityObj.options.length=0;
        //Determine whether a province is selected if (province!=""){
            let cityOption = new Option("---Please select a city---","");
            cityObj.options.add(cityOption);
        }
        //According to the province, get the corresponding city array and traverse the city array for (let city of cityArr[province]){
            //Populate each city into the city list let cityOption = new Option(city,city);
            cityObj.options.add(cityOption)
        }
    }
</script>
</body>
</html>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • JavaScript realizes the linkage effect between provinces and cities
  • JavaScript to achieve provincial and municipal linkage bug solution
  • Code sharing based on JS to achieve provincial and municipal linkage effect
  • js provincial and municipal linkage effect complete example code
  • JSON+HTML realizes the linkage selection effect of country, province and city
  • Province-city linkage menu implemented by JavaScript two-dimensional array
  • JavaScript provincial and municipal linkage implementation code
  • A simple example of using js to achieve the linkage effect between provinces and cities
  • javascript 2009 latest version of provincial and municipal linkage
  • JavaScript to achieve simple provincial and municipal linkage

<<:  How to correctly create MySQL indexes

>>:  SQL statements in Mysql do not use indexes

Recommend

Javascript Basics: Detailed Explanation of Operators and Flow Control

Table of contents 1. Operator 1.1 Arithmetic oper...

Detailed explanation of the use of Arguments object in JavaScript

Table of contents Preface Basic Concepts of Argum...

Native JavaScript to achieve the effect of carousel

This article shares the specific code for JavaScr...

Detailed explanation of Linux index node inode

1. Introduction to inode To understand inode, we ...

How to periodically clean up images that are None through Jenkins

Preface In the process of continuous code deliver...

Using CSS to implement image frame animation and curve motion

The basic principle of all animations is to displ...

JS uses the reduce() method to process tree structure data

Table of contents definition grammar Examples 1. ...

mysql indexof function usage instructions

As shown below: LOCATE(substr,str) Returns the fi...

Detailed introduction to the MySQL installation tutorial under Windows

Table of contents 1. Some concepts you need to un...

Introduction to JavaScript strict mode use strict

Table of contents 1. Overview 1.1 What is strict ...

VMware 15.5 version installation Windows_Server_2008_R2 system tutorial diagram

1. Create a new virtual machine from VMware 15.5 ...

Detailed tutorial on installing MySQL database on Alibaba Cloud Server

Table of contents Preface 1. Uninstall MySQL 2. I...

Examples of implementing progress bars and order progress bars using CSS

The preparation for the final exams in the past h...

How to build LNMP environment on Ubuntu 20.04

Simple description Since it was built with Centos...

SQL query for users who have logged in for at least n consecutive days

Take 3 consecutive days as an example, using the ...