JavaScript two-level linkage

Source: Internet
Author: User

Second-level linkage in general web pages everywhere, generally is the address, such as click on Zhejiang province, followed by Hangzhou, Jiaxing; Click on the Beijing province appears Chaoyang, Haidian, instead of appearing in Hangzhou, Jiaxing.

To implement this step, JavaScript is used. Where the principle is used to onchange time.

First, the onchange event occurs when the contents of the domain change. JavaScript objects that support this event: FileUpload, select, text, textarea, we are using Select to achieve level two linkage.

The following is the HTML code, first set a select for the province, the second select for the city, but the city we use an array in JS to connect it with the province.

<! DOCTYPE html>

Here is the JS code

var province = document.getElementById ("province"), var city = document.getElementById ("city"), var area = [[' Chaoyang ', ' Haidian ', ' Beijing '],//array of the No. 0 area. 0{0,1,2}[' Hangzhou ', ' haining ']//1th area array, 1{0.1}];     function choose () {var opt = "";  var len = Area[province.value]; If you choose Beijing 0, then, len=[' Chaoyang ', ' Haidian ' This is the link which province corresponds to which cityCity'sThe array if (Province.value = = '-1 ') {//Because the value of select IS-1 is the word ' province ' instead of Beijing, so when we choose this province it corresponds to let his city be empty city.innerhtml =           Opt } for (var i = 0;i < Len.length; i++) {//area array number for (i = 0;i < 3; i++) opt = opt + ' <option value = "'                                                                        + i + ' "> ' + len[i]+ ' </option> '//opt =" "+ <option value =" 0 "> Chaoyang (lin[0]) </option> opt = <option value = "0" > Chaoyang (lin[0]) </option>
<option value = "1" > Haidian (lin[1]) </option> opt = <option value = "0" > Chaoyang (lin[0]) </option>
<option value = "1" > Haidian (lin[1]) </option> +
<option value = "2" > Beijing (lin[2]) </option>} city.innerhtml = opt;} Province.onchange = function () {choose ();}

Second-level linkage in general web pages everywhere, generally is the address, such as click on Zhejiang province, followed by Hangzhou, Jiaxing; Click on the Beijing province appears Chaoyang, Haidian, instead of appearing in Hangzhou, Jiaxing.

To implement this step, JavaScript is used. Where the principle is used to onchange time.

JavaScript two-level linkage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.