1. Create a test XML file: Select. xml
CopyCode The Code is as follows: <? XML version = "1.0" encoding = "GBK"?>
<SELECT>
<Province id = "SX">
Shaanxi
<City id = "xa"> Xi'an </city>
<City id = "BJ"> Baoji </city>
<City id = "AK"> Ankang </city>
</Province>
<Province id = "JS">
Jiangsu
<City id = "NJ"> Nanjing </city>
<City id = "xz"> Xuzhou </city>
</Province>
<Province id = "sh">
Shanghai
</Province>
</SELECT>
2. Create an HTML page: select.html Copy code The Code is as follows: <body>
</Body>
<SCRIPT>...
/**//**
* @ Description Level-2 cascade drop-down
* @ Author blueslee
* @ Lastmodif blueslee
* @ Createdate 2007-10-13
* @ Modifdate 2007-10-15
* @ Version 1.0
*/
Selectlevel. Prototype. xml;
Selectlevel. Prototype. Provinces;
Selectlevel. Prototype. parentname = "Province"; // parent node name
Selectlevel. Prototype. childname = "city"; // subnode name
Selectlevel. Prototype. keyname = "ID"; // attribute
/**//**
* Object selectlevel Constructor
* @ Author blueslee
* @ Lastmodif blueslee
* @ Createdate 2007-10-13
* @ Modifdate 2007-10-15
* @ Version 1.0
*/
Function selectlevel (parentname, childname, keyname )...{
If (parentname! = NULL & parentname! = "")...{
This. parentname = parentname;
}
If (childname! = NULL & childname! = "")...{
This. childname = childname;
}
If (keyname! = NULL & keyname! = "")...{
This. keyname = keyname;
}
}
/**//**
* Load XML files
* @ Author blueslee
* @ Lastmodif blueslee
* @ Createdate 2007-10-13
* @ Modifdate 2007-10-15
* @ Version 1.0
*/
Selectlevel. Prototype. readxml = function (URL )...{
VaR selectxml;
// If it is supported, use standard level 2 DOM technology
If (document. Implementation & document. Implementation. createdocument )...{
// Create a new document object
Selectxml = Document. Implementation. createdocument ("", "", null );
// Set the trigger event upon completion of loading
Selectxml. onload = function ()...{
Alert ("loaded ");
}
Selectxml. Load (URL );
} Else... {// create a Document Object in IE browser
Selectxml = new activexobject ("Microsoft. xmldom ");
// Set onload
Selectxml. onreadystatechange = function ()...{
If (selectxml. readystate = 4 )...{
Alert ("loaded ");
}
}
Selectxml. Load (URL );
}
This. xml = selectxml;
}
/**//**
* Traverse XML
* @ Author blueslee
* @ Lastmodif blueslee
* @ Createdate 2007-10-13
* @ Modifdate 2007-10-15
* @ Version 1.0
*/
Selectlevel. Prototype. iteratorxml = function (node, level )...{
VaR n = node;
If (n = NULL )...{
Nw.this.xml.doc umentelement;
}
If (Level = NULL )...{
Level = 0;
}
If (N. nodetype = 3 )...{
For (VAR I = 0; I <level; I ++ )...{
Document. Write ("-");
}
Document. Write (N. Data. Trim () + "<br> ");
} Else ...{
For (var m = n. firstchild; m! = NULL; M = M. nextsibling )...{
This. iteratorxml (M, level + 1 );
}
}
}
/**//**
* Pull-down Association
* @ Author blueslee
* @ Lastmodif blueslee
* @ Createdate 2007-10-13
* @ Modifdate 2007-10-15
* @ Version 1.0
*/
Selectlevel. Prototype. changeselect = function ()...{
VaR city = Document. getelementbyid (this. childname );
VaR province = Document. getelementbyid (this. parentname );
City. Options. Length = 0;
If (province. value = NULL | province. value = "")...{
City. Options. Length = 1;
City. Options [0]. Text = "Please select... ";
Return;
}
VaR citys = This. provinces [this [province. value]. getelementsbytagname (this. childname );
If (Citys. Length = 0 )...{
City. Options. Length = city. Options. Length + 1;
City. Options [city. Options. Length-1]. value = province. value;
For (VAR I = 0; I <province. Options. length; I ++ )...{
If (province. Options [I]. Selected )...{
City. Options [city. Options. Length-1]. Text = province. Options [I]. text;
Return;
}
}
Return;
}
City. Options. Length = Citys. length;
For (VAR I = 0; I <Citys. length; I ++ )...{
City. Options [I]. value = citys [I]. getattribute (this. keyname );
City. Options [I]. Text = citys [I]. firstchild. Data. Trim ();
}
}
/**//**
* Initialization drop-down list
* @ Author blueslee
* @ Lastmodif blueslee
* @ Createdate 2007-10-13
* @ Modifdate 2007-10-15
* @ Version 1.0
*/
Selectlevel. Prototype. init = function (parent, province, city )...{
This. Provinces = This. xml. getelementsbytagname (this. parentname );
VaR selectprovince = Document. createelement ("select ");
VaR selectcity = Document. createelement ("select ");
VaR OBJ = this;
Selectprovince. setattribute ("name", this. parentname );
Selectprovince. setattribute ("ID", this. parentname );
Selectprovince. attachevent ("onchange", function ()... {obj. changeselect ();});
Selectprovince. Options. Length = This. Provinces. Length + 1;
Selectprovince. Options [0]. Text = "select... ";
Selectcity. setattribute ("name", this. childname );
Selectcity. setattribute ("ID", this. childname );
Selectcity. Options. Length = 1;
Selectcity. Options [0]. Text = "select... ";
For (VAR I = 0; I <this. Provinces. length; I ++ )...{
Selectlevel. Prototype [This. provinces [I]. getattribute (this. keyname)] = I;
Selectprovince. Options [I + 1]. value = This. provinces [I]. getattribute (this. keyname );
Selectprovince. Options [I + 1]. Text = This. provinces [I]. firstchild. Data. Trim ();
If (this. provinces [I]. getattribute (this. keyname) = province )...{
Selectprovince. Options [I + 1]. Selected = true;
VaR citys = This. provinces [I]. getelementsbytagname (this. childname );
Selectcity. Options. Length = Citys. Length + 1;
For (var j = 0; j <Citys. length; j ++ )...{
Selectcity. Options [J + 1]. value = citys [J]. getattribute (this. keyname );
Selectcity. Options [J + 1]. Text = citys [J]. firstchild. Data. Trim ();
If (citys [J]. getattribute (this. keyname) = city )...{
Selectcity. Options [J + 1]. Selected = true;
}
}
}
}
Parent. appendchild (selectprovince );
Parent. appendchild (selectcity );
}
String. Prototype. Trim = function ()...{
Return this. Replace (/^ s */g, ""). Replace (/S * $/g ,"");
}
// Test
VaR newxml = new selectlevel ();
Newxml. readxml ("Select. xml ");
// Newxml. iteratorxml (null,-2 );
Newxml. INIT (document. Body, "SX", "BJ ");
</SCRIPT>