制作联动下拉框

<html>
<head>
<title>liandong menu</title>
<script type="text/javascript" src="loadxmldoc.js"></script>
</head>
<script>
var root;
function selectProvice()
{
	var source;
	var sourceName = "citys.xml";
	var source=loadXMLDoc(sourceName);
	root=source.documentElement;//设置文档元素为根节点元素
	nodes=root.selectNodes("//@name");//搜索属性中含有name的所有结点
	for(var i=0;i<nodes.length;++i)
	{
		var oOption=document.createElement('OPTION');
		oOption.text= ' ' +nodes[i].text+" ";
		oOption.value=nodes[i].text;
		form1.selProvice.options.add(oOption);//add????????????????????
	}
	selectCity();
}
function selectCity()
{
	//在selCity中删除
	for(var i=form1.selCity.options.length-1;i>=0;--i)
	{
		form1.selCity.options.remove(i);
	}
	x=form1.selProvice.selectedIndex;//读取省份下拉框的当前选项
	
	y=form1.selProvice.options[x].value;
	//搜索name属性值等于参数结点下的所有结点
	nodes=root.selectNodes("//province[@name='"+y+"']/city");
	for(var i=0;i<nodes.length;++i)//增加城市名称到下拉列表中
	{
		var oOption=document.createElement("OPTION");
		oOption.text=" "+nodes[i].text+" ";
		oOption.value=nodes[i].text;
		form1.selCity.options.add(oOption);
	}
}
</script>
<body οnlοad="selectProvice();">
<form action=""  method="post" id="form1" name="form1">
	<select name="selProvice" id="selProvice" οnchange="selectCity();">
    </select>
    <select name="selCity" id="selCity">
    </select>
</form>

</body>
</html>


citys.xml

<?xml version="1.0" encoding="gb2312" ?>
<china>
	<province id="1" name="beijng">
	<city>xichengqu</city>
	<city>dongchengqu</city>
	<city>chaoyangqu</city>
	<city>fengtaiqu</city>
	</province>
	
	<province id="2" name="hubei">
	<city>wuhan</city>
	<city>jingzhou</city>
	<city>xiaogan</city>
	<city>xiantao</city>
	</province>
	
	<province id="3" name="fujian">
	<city>fuzhou</city>
	<city>xiamen</city>
	</province>
	
	<province id="4" name="gansu">
	<city>lanzhou</city>
	</province>
	
	
	<province id="5" name="guangdong">
	<city>guanghzhou</city>
	<city>shenzhen</city>
	<city>dongguan</city>
	<city>huizhou</city>
	</province>
</china>

loadxmldoc.js

function loadXMLDoc(dname) 
{
try //Internet Explorer
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  }
catch(e)
  {
  try //Firefox, Mozilla, Opera, etc.
    {
    xmlDoc=document.implementation.createDocument("","",null);
    }
  catch(e) {alert(e.message)}
  }
try 
  {
  xmlDoc.async=false;
  xmlDoc.load(dname);
  return(xmlDoc);
  }
catch(e) {alert(e.message)}
return(null);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值