<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>My First Ajax</TITLE>
</HEAD>
<script>
var xmlHttp;
var ProID;
var CityID;
var ProNode;
var CityNode;
function createXMLHttpRequest()
{
if(window.ActiveXObject)
{
xmlHttp=new window.ActiveXObject("Microsoft.XMLHttp");
}
else if(window.XMLHttpRequest)
{
xmlHttp=new window.XMLHttpRequest();
}
}
function ProLoad()
{
createXMLHttpRequest();
xmlHttp.OnReadyStateChange=HandleStateChange;
xmlHttp.open("Get","area.xml",true);
xmlHttp.send(null);
}
function CityLoad()
{
ProID=document.ajax.ProSelect.value;
//alert(ProID);
if (!xmlHttp)
{
xmlHttp.abort();
}
createXMLHttpRequest();
xmlHttp.OnReadyStateChange=HandleStateChange2;
xmlHttp.open("Get","area.xml",true);
xmlHttp.send(null);
}
function AreaLoad()
{
CityID=document.ajax.CitySelect.value;
//alert(CityID);
if (!xmlHttp)
{
xmlHttp.abort();
}
createXMLHttpRequest();
xmlHttp.OnReadyStateChange=HandleStateChange3;
xmlHttp.open("Get","area.xml",true);
xmlHttp.send(null);
}
function HandleStateChange()
{
if(xmlHttp.ReadyState==4)
{
if(xmlHttp.Status==200)
{
try
{
pro();
}
catch (exception)
{
alert("error");
}
}
}
}
function HandleStateChange2()
{
if(xmlHttp.ReadyState==4)
{
if(xmlHttp.Status==200)
{
try
{
ProChagnge();
}
catch (exception)
{
alert("error");
}
}
}
}
function HandleStateChange3()
{
if(xmlHttp.ReadyState==4)
{
if(xmlHttp.Status==200)
{
try
{
CityChagnge();
//alert(CityID);
}
catch (exception)
{
alert("error");
}
}
}
}
function pro()
{
var select = document.getElementById("ProSelect");
while (select.length > 0)
{
select.remove(0);
}
document.ajax.ProSelect.add(document.createElement("OPTION"));
document.ajax.ProSelect.options[0].text="請選擇省份";
document.ajax.ProSelect.options[0].value="請選擇省份";
var xmlDoc=xmlHttp.responseXML;
var proNode=xmlDoc.getElementsByTagName("province");
for(i=0;i<proNode.length;i++)
{
//alert(proNode[i].getAttribute("name"))
document.ajax.ProSelect.add(document.createElement("OPTION"));
document.ajax.ProSelect.options[i+1].text=proNode[i].getAttribute("name");
document.ajax.ProSelect.options[i+1].value=proNode[i].getAttribute("id");
}
}
function ProChagnge()
{
var select = document.getElementById("CitySelect");
while (select.length > 0)
{
select.remove(0);
}
document.ajax.CitySelect.add(document.createElement("OPTION"));
document.ajax.CitySelect.options[0].text="請選擇城市";
document.ajax.CitySelect.options[0].value="no";
var select = document.getElementById("AreaSelect");
while (select.length > 0)
{
select.remove(0);
}
document.ajax.AreaSelect.add(document.createElement("OPTION"));
document.ajax.AreaSelect.options[0].text="請選擇";
document.ajax.AreaSelect.options[0].value="no";
var xmlDoc=xmlHttp.responseXML;
var proNode=xmlDoc.getElementsByTagName("province");
for(i=0;i<proNode.length;i++)
{
if (proNode[i].getAttribute("id")==ProID)
{
var proNode2=xmlDoc.getElementsByTagName("province")[i];
break;
}
}
CityNode=proNode2.getElementsByTagName("city");
for(i=0;i<CityNode.length;i++)
{
//alert(proNode[i].getAttribute("name"))
if(CityNode[i].getAttribute("id")!="")
{
document.ajax.CitySelect.add(document.createElement("OPTION"));
document.ajax.CitySelect.options[i+1].text=CityNode[i].getAttribute("name");
document.ajax.CitySelect.options[i+1].value=CityNode[i].getAttribute("id");
}
}
}
function CityChagnge()
{
var select = document.getElementById("AreaSelect");
while (select.length > 0)
{
select.remove(0);
}
if(document.ajax.CitySelect.value!="no")
{
for(i=0;i<CityNode.length;i++)
{
if (CityNode[i].getAttribute("id")==CityID)
{
//alert(i);
var CityNode2=CityNode[i];
break;
}
}
var AreaNode=CityNode2.getElementsByTagName("area");
for(i=0;i<AreaNode.length;i++)
{
if(AreaNode[i].getAttribute("id")!="")
{
document.ajax.AreaSelect.add(document.createElement("OPTION"));
document.ajax.AreaSelect.options[i].text=AreaNode[i].childNodes[0].nodeValue;
document.ajax.AreaSelect.options[i].value=AreaNode[i].getAttribute("id");
}
}
}
else
{
document.ajax.AreaSelect.add(document.createElement("OPTION"));
document.ajax.AreaSelect.options[0].text="請選擇";
document.ajax.AreaSelect.options[0].value="no";
}
}
</script>
<body onload="ProLoad()">
<form action=# name="ajax" >
<select name="ProSelect" onchange="CityLoad()">
<option>請選擇省份</option>
</select>
<select name="CitySelect" onchange="AreaLoad()">
<option>請選擇城市</option>
</select>
<select name="AreaSelect">
<option>請選擇</option>
</select>
</form>
</BODY>
</HTML>