JavaScript linkage of the infinite class package, data using non-Ajax way, random add linkage _javascript skills

Source: Internet
Author: User
HTML code
Copy Code code as follows:



<meta http-equiv= ' content-type ' content= ' text/html; Charset=utf-8 '/>
<title> Try </title>

<script type= "Text/javascript" >
var data = {
' 1 ': ' a001 ',
' 2 ': ' a002 ',
' 3 ': ' a003 ',
' 4 ': ' a004 ',
' 5 ': ' a005 ',
' 6 ': ' a006 '
};
var data2 = {
' 1 ': {' a101 ', ' 102 ': ' a102 ', ' The ', ': ' a103 ', ' The ' ': ' a104 '},
' 2 ': {' 201 ': ' a201 ', ' a202 ', ' 203 ': ' a203 ', ' 204 ': ' a204 '},
' 3 ': {' a301 ', ' 302 ': ' a302 ', ' 303 ': ' A303 ', ' 304 ': ' a304 '},
' 4 ': {' 401 ': ' a401 ', ' 402 ': ' a402 ', ' 403 ': ' a403 ', ' 404 ': ' A404 '},
' 5 ': {' 501 ': ' a501 ', ' 502 ': ' a502 ', ' 503 ': ' a503 ', ' 504 ': ' a504 '},
' 6 ': {' 601 ': ' a601 ', ' 602 ': ' a602 ', ' 603 ': ' a603 ', ' 604 ': ' a604 '},
' I ': {' 10101 ': ' a10101 ', ' 10102 ': ' a10102 ', ' 10103 ': ' a10103 ', ' 10104 ': ' a10104 '},
' 102 ': {' 10201 ': ' a10201 ', ' 10202 ': ' a10202 ', ' 10203 ': ' a10203 ', ' 10204 ': ' a10204 '},
' 10301 ': {' a10301 ', ' 10302 ': ' a10302 ', ' 10303 ': ' a10303 ', ' 10304 ': ' a10304 '},
' 10401 ': {' a10401 ', ' 10402 ': ' a10402 ', ' 10403 ': ' a10403 ', ' 10404 ': ' a10404 '},
' 201 ': {' 20101 ': ' a20101 ', ' 20102 ': ' a20102 ', ' 20103 ': ' a20103 ', ' 20104 ': ' a20104 '},
' 20201 ': ' a20201 ', ' 20202 ': ' a20202 ', ' 20203 ': ' a20203 ', ' 20204 ': ' a20204 '},
' 203 ': {' 20301 ': ' a20301 ', ' 20302 ': ' a20302 ', ' 20303 ': ' a20303 ', ' 20304 ': ' a20304 '},
' 204 ': {' 20401 ': ' a20401 ', ' 20402 ': ' a20402 ', ' 20403 ': ' a20403 ', ' 20404 ': ' a20404 '},
' 30101 ': ' a30101 ', ' 30102 ': ' a30102 ', ' 30103 ': ' a30103 ', ' 30104 ': ' a30104 '},
' 302 ': {' 30201 ': ' a30201 ', ' 30202 ': ' a30202 ', ' 30203 ': ' a30203 ', ' 30204 ': ' a30204 '},
' 303 ': {' 30301 ': ' a30301 ', ' 30302 ': ' a30302 ', ' 30303 ': ' a30303 ', ' 30304 ': ' a30304 '},
' 304 ': {' 30401 ': ' a30401 ', ' 30402 ': ' a30402 ', ' 30403 ': ' a30403 ', ' 30404 ': ' a30404 '},
' 401 ': {' 40101 ': ' a40101 ', ' 40102 ': ' a40102 ', ' 40103 ': ' a40103 ', ' 40104 ': ' a40104 '},
' 402 ': {' 40201 ': ' a40201 ', ' 40202 ': ' a40202 ', ' 40203 ': ' a40203 ', ' 40204 ': ' a40204 '},
' 403 ': {' 40301 ': ' a40301 ', ' 40302 ': ' a40302 ', ' 40303 ': ' a40303 ', ' 40304 ': ' a40304 '},
' 404 ': {' 40401 ': ' a40401 ', ' 40402 ': ' a40402 ', ' 40403 ': ' a40403 ', ' 40404 ': ' a40404 '},
' 501 ': {' 50101 ': ' a50101 ', ' 50102 ': ' a50102 ', ' 50103 ': ' a50103 ', ' 50104 ': ' a50104 '},
' 502 ': {' 50201 ': ' a50201 ', ' 50202 ': ' a50202 ', ' 50203 ': ' a50203 ', ' 50204 ': ' a50204 '},
' 503 ': {' 50301 ': ' a50301 ', ' 50302 ': ' a50302 ', ' 50303 ': ' a50303 ', ' 50304 ': ' a50304 '},
' 504 ': {' 50401 ': ' a50401 ', ' 50402 ': ' a50402 ', ' 50403 ': ' a50403 ', ' 50404 ': ' a50404 '},
' 601 ': {' 60101 ': ' a60101 ', ' 60102 ': ' a60102 ', ' 60103 ': ' a60103 ', ' 60104 ': ' a60104 '},
' 602 ': {' 60201 ': ' a60201 ', ' 60202 ': ' a60202 ', ' 60203 ': ' a60203 ', ' 60204 ': ' a60204 '},
' 603 ': {' 60301 ': ' a60301 ', ' 60302 ': ' a60302 ', ' 60303 ': ' a60303 ', ' 60304 ': ' a60304 '},
' 604 ': {' 60401 ': ' a60401 ', ' 60402 ': ' a60402 ', ' 60403 ': ' a60403 ', ' 60404 ': ' a60404 '}
};
window.$ = function (ID) {
if (typeof id = = ' string ') {
return document.getElementById (ID);
}
return ID;
}
/**
* Event Handling Tool class
*
* @author bao110908
*/
var Event = function () {}
Event = {
/**
* Add to Event event for element using a handler handler
* Compatible browsers such as IE and Firefox
*
* For example, add the onclick event for the Botton object, using the Clickevent
* Method as Handler:
* Event.addevent (Botton, ' click ', clickevent);
*
* @param element needs to add an event (object)
* @param event name (String) to be added without "on"
* @param handler the method reference (Function) that needs to be added
*/
Addevent:function (element, event, handler) {
if (element.attachevent) {
Element.attachevent (' on ' + event, handler);
else if (Element.addeventlistener) {
Element.addeventlistener (event, Handler, false);
} else {
element[' on ' + event] = handler;
}
},
/**
* When adding an event handler, you can only add a reference to the method, and you cannot give
* Method plus parameters. For example, the definition of the clickevent (str) method, now
* When you want to use it as the OnClick event handler for obj, you can use:
* Obj.onclick = Event.getfuntion (null, clickevent, str);
*/
Getfunction:function (obj, fun) {
var args = [];
obj = obj | | Window
for (var i = 2; i < arguments.length; i++) {
Args.push (Arguments[i]);
}
return function () {
Fun.apply (obj, args);
};
}
}
/**
* Linkage Processing Class
*
* @param ID of First select
* @param second The ID of the second select
* @param data for the second select of a DataSet
*
* @author bao110908
*/
var linkage = function (A, second) {
This.first = $ (a);
This.second = $ (second);
}
/**
* Initialize data for the first select
*/
Linkage.initfirst = function (A, dataSet) {
var base = $ (a);
Base.options.length = 1;
For (var k in DataSet) {
var opt = new Option (Dataset[k], k);
Base.options[base.options.length] = opt;
}
}
Linkage.prototype = {
Class
Init:function () {
This.addonchange ();
},
Add a onchange event for the first select
Addonchange:function () {
Event.addevent (This.first, ' Change ', event.getfunction (this, this._onchangeevent));
},
onchange Event Handling
_onchangeevent:function () {
This._defaultselect ();
var data = This._getdata (This.first.value);
if (!data) {
Return
}
This.second.options.length = 1;
For (var k in data) {
var opt = new Option (Data[k], k);
This.second.options[this.second.options.length] = opt;
}
},
Get the data, if you are using Ajax, you need to make changes
Ajax should return {' a101 ': {' ' 102 ': ' ' the ', ', ', ' a102 '}} data format
Then use eval (' + Ajaxdata + '); Convert to JSON Object
_getdata:function (value) {
return Data2[value];
},
Processing when choosing a new selection
_defaultselect:function () {
This.second.selectedIndex = 0;
This.second.options.length = 1;
if (this.second.fireEvent) {
Ie
This.second.fireEvent (' onchange ');
} else {
DOM 2
var event = document.createevent (' htmlevents ');
Event.initevent (' Change ', false, true);
This.second.dispatchEvent (event);
}
}
}
Window.onload = function () {
Linkage.initfirst (' base1 ', data);
var one = new linkage (' base1 ', ' base2 ');
One.init ();
var two = new linkage (' base2 ', ' base3 ');
Two.init ();
}
</script>
<style type= "Text/css" >
* {
font-size:12px;
Font-family: "Courier New";
}
Select {
width:120px;
}
</style>
<body>
Level: <select name= "Base1" id= "base1" ><option value= "0" >--Please select--</option></select>

Level two: <select name= "Base2" id= "Base2" ><option value= "0" >--Please select--</option></select>

Level Three: <select name= "Base3" id= "base3" ><option value= "0" >--Please select--</option></select>
</body>


JS Code
Copy Code code as follows:

var one = new linkage (' base1 ', ' base2 ', data2);
One.init ();
var two = new linkage (' base2 ', ' base3 ', data3);
Two.init ();
Each additional level of linkage, it is necessary to new.

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.