Use the jquery anonymous inner class to implement the Drop-down list three-level linkage

Source: Internet
Author: User


<title>3434</title>
<script src= "Js/jquery-1.9.1.js" type= "Text/javascript" ></script>
<script src= "Js/jquery.cookie.js" type= "Text/javascript" ></script>
<script type= "Text/javascript" >
JQuery (function () {
function Pos () {
this.item={};
};
Pos.prototype.add = function (id,value) {
This.item[id] = value;
}

var pos = new Pos ();
Pos.add (' 0 ', [' Beijing ', ' Shanghai ']);
Pos.add (' 0_0 ', [' Dongcheng District ', ' Xicheng ']);
Pos.add (' 0_0_0 ', [' Hutong 1 ', ' Hutong 2 ', ' Hutong 3 ', ' Hutong 4 ']);
Pos.add (' 0_0_1 ', [' Hutong 5 ', ' Hutong 6 ', ' Hutong 7 ', ' Hutong 8 ']);
Pos.add (' 0_1 ', [' Pudong District ', ' Puxi area ']);
Pos.add (' 0_1_0 ', [' Hutong 9 ', ' Hutong 10 ', ' Hutong 11 ', ' Hutong 12 ']);
Pos.add (' 0_1_1 ', [' Hutong 13 ', ' Hutong 14 ', ' Hutong 15 ', ' Hutong 16 ']);


function init () {
var $op =$ (' <option></option> ');
var sharr= pos.item[' 0 '];
for (var i = 0; i < sharr.length; i++) {
var $op =$ (' <option></option> ');
$op. Val (Sharr[i]);
$op. Text (Sharr[i]);
$ (' #sheng '). Append ($op);
};
}
Init ();
$ ("select"). Change (function () {
var index = $ ("select"). Index ($ (this));
if (index==0) {
var ind=$ (this). Get (0). SelectedIndex;
if ((ind-1) >=0) {
var Shiarr = pos.item[' 0_ ' + (ind-1)];
Clearops ($ (' #shi '));
for (var i = 0; i < shiarr.length; i++) {
var $op =$ (' <option></option> ');
$op. Val (Shiarr[i]);
$op. Text (Shiarr[i]);
$ (' #shi '). Append ($op);
};
}
}


if (index==1) {
var prevind=$ (' #sheng '). Get (0). SelectedIndex;
var ind=$ (this). Get (0). SelectedIndex;
if ((ind-1) >=0) {
var Xianarr = pos.item[' 0_ ' + (prevind-1) + ' _ ' + (ind-1)];
Clearops ($ (' #xian '));
for (var i = 0; i < xianarr.length; i++) {
var $op =$ (' <option></option> ');
$op. Val (Xianarr[i]);
$op. Text (Xianarr[i]);
$ (' #xian '). Append ($op);
};
}
}
});


function Clearops ($sel) {
$sel. Find ("Option:not (: eq (0))"). Remove ();
};
});

</script>

<body>
<div>
<select id= ' Sheng ' >
<option value= ' Select ' >Select</option>
</select>
<select id= ' Shi ' >
<option value= ' Select ' >Select</option>
</select>
<select id= ' Xian ' >
<option value= ' Select ' >Select</option>
</select>
</div>
</body>




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.