<script>
function ShowCity(province)
{
//清空下拉框的值
document.getElementByIdx("son_name").length=0;
document.getElementByIdx("son_name").options[0] = new Option("--请选择城市--","");
// 或者循环清空
// for(var i = document.getElementByIdx("son_name").length - 1; i > 0; i--) {
// document.getElementByIdx("son_name").remove(document.getElementByIdx("son_name").options[i]);
// }
//得到选中的省份
var selectprovince = province.options[province.selectedIndex].value;
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.load("XMLFile.XML");
//得到xml中的所有省份
var provincexml =xmlDoc.documentElement.childNodes;
//循环判断哪个是你选中的
for(i=0;i<provincexml.length;i++)
{
if(provincexml[i].attributes[0].value == selectprovince)
{
//给下拉框循环添加市
for(j = 0;j < provincexml[i].childNodes.length; j++)
{
var city = provincexml[i].childNodes[j].text;
document.getElementByIdx("son_name").options[document.getElementByIdx("son_name").length] = new Option(city,city);
}
break;
}
}
}
</script>
<body>
<form id="form1" runat="server">
<div>
<select id="father_name" οnchange="ShowCity(this)">
<option value="" selected="selected" >--请选择省份--</option>
<option value="江苏">江苏</option>
<option value="浙江">浙江</option>
</select>
<select id="son_name">
<option>--请选择城市--</option>
</select>
</div>
</form>
</body>
XMLFile.xml文件:
<?xml version="1.0" encoding="utf-8" ?>
<country>
<province attr="江苏">
<city>徐州</city>
<city>南京</city>
</province>
<province attr="浙江">