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:
|
<<: How to correctly create MySQL indexes
>>: SQL statements in Mysql do not use indexes
Table of contents 1. Operator 1.1 Arithmetic oper...
Table of contents Preface Basic Concepts of Argum...
This article shares the specific code for JavaScr...
1. Introduction to inode To understand inode, we ...
Preface In the process of continuous code deliver...
The basic principle of all animations is to displ...
Table of contents definition grammar Examples 1. ...
As shown below: LOCATE(substr,str) Returns the fi...
Table of contents 1. Some concepts you need to un...
Table of contents 1. Overview 1.1 What is strict ...
1. Create a new virtual machine from VMware 15.5 ...
Table of contents Preface 1. Uninstall MySQL 2. I...
The preparation for the final exams in the past h...
Simple description Since it was built with Centos...
Take 3 consecutive days as an example, using the ...