1. Create a test XML file: select. xml
Copy codeThe 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 codeThe 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>