Home > Web Front-end > JS Tutorial > JavaScript implements simple secondary linkage_javascript skills

JavaScript implements simple secondary linkage_javascript skills

WBOY
Release: 2016-05-16 16:08:31
Original
1230 people have browsed it

Second-level linkage can be seen everywhere in general web pages, usually addresses. For example, if you click on Zhejiang Province, Hangzhou and Jiaxing cities will appear; if you click on Beijing Province, Chaoyang and Haidian will appear instead of Hangzhou and Jiaxing.

To achieve this step, you need to use javascript. The principle uses onchange time.

First, the onchange event occurs when the content of the field changes. JavaScript objects that support this event: fileUpload, select, text, textarea. We use select to implement secondary linkage.

The following is the HTML code. First, set one select to be the province, and the second select to be the city. But for the city, we use an array in js to connect it to the province.

Copy code The code is as follows:





javascript secondary linkage







The following is the js code

Copy code The code is as follows:

var province = document.getElementById("province");
var city = document.getElementById("city");
var area = [
['Chaoyang','Haidian','Beijing'], //The array of the 0th area. 0{0,1,2}
['Hangzhou','Haining'] //Array of the first area, 1{0.1}
];
function choose(){
var opt = "";
var len = area[province.value]; //If Beijing 0 is selected, then len=['Chaoyang','Haidian'] This is to connect which province corresponds to which cityCityarray
if(province.value == '-1'){ //Because when the value of select is -1, it is the word 'province', not Beijing, so when we select this province, let its city be Empty
           city.innerHTML = opt;
}
for(var i = 0;i < len.length; i ){ //The number of arrays in area for(i = 0;i < 3; i )
opt = opt '' //opt = "" ,
//opt = ,
                 

//opt = ,
                 

                 

}
City.innerHTML = opt;
}
province.onchange = function(){
Choose();
}

Second-level linkage can be seen everywhere in general web pages, usually addresses. For example, if you click on Zhejiang Province, Hangzhou and Jiaxing cities will appear; if you click on Beijing Province, Chaoyang and Haidian will appear instead of Hangzhou and Jiaxing.

To achieve this step, you need to use javascript. The principle uses onchange time.

The above is the entire content of this article, I hope you all like it.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template