200分:javascript读XML节点问题(省市三级联动)
我有一个省市二级联动的程序如下,希望改成三级联动,读3J.XML的内容将格式变为:北京市市辖区宣武区这样联动,请问如何修改呢?3j.html<scriptlanguage...
我有一个省市二级联动的程序如下,希望改成三级联动,读3J.XML的内容
将格式变为:北京市 市辖区 宣武区 这样联动,请问如何修改呢?
3j.html
<script language="JavaScript">
function ChooseState()
{
var source;
var sourceName = "3j.xml";
var source = new ActiveXObject('Microsoft.XMLDOM');
source.async = false;
source.load(sourceName); //装入XML文档
root = source.documentElement; //设置文档元素为根节点元素
sortField=root.selectNodes("//@name"); //搜索属性中含有name的所有节点
for(var i=0;i<sortField.length;++i) //增加省份名称到下拉列表
{
var oOption = document.createElement('OPTION');
oOption.text = " "+sortField[i].text+" ";
oOption.value = sortField[i].text;
form1.SelState.options.add(oOption);
}
ChooseCity();
}
//自定义函数:ChooseCity
//(根据当前选定的省名来读取XML数据中的对应城市名称,并增加到SelCity 的下拉列表中)
function ChooseCity()
{
x=form1.SelState.selectedIndex; //读取省份下拉框的当前选项
y=form1.SelState.options[x].value;
sortField=root.selectNodes("//State[@name='"+y+"']/City"); //搜索name属性值等于参数y的State节点下的所有city节点
for(var i=form1.SelCity.options.length-1;i>=0;--i) //撤消原来的列表项
{
form1.SelCity.options.remove(i)
}
for(var i=0;i<sortField.length;++i) //增加城市名称到下拉列表
{
var oOption = document.createElement('OPTION');
oOption.text = " "+sortField[i].text+" ";
oOption.value = sortField[i].text;
form1.SelCity.options.add(oOption);
}
}</script>
<BODY onLoad="ChooseState()">
<FORM action="" method="post" id="form1" name="form1">
<SELECT name="SelState" id="SelState" onchange="ChooseCity()" >
</SELECT>
<SELECT name="SelCity" id="SelCity" >
</SELECT>
</FORM>
</BODY>
3j.xml
<?xml version="1.0" encoding="GB2312" ?>
<district>
<prov id="110000" text="北京市">
<city id="110100" text="市辖区">
<county id="110101" text="东城区"></county>
</city>
<city id="110200" text="县">
<county id="110228" text="密云县"></county>
<county id="110229" text="延庆县"></county>
</city>
</prov>
<prov id="120000" text="天津市">
<city id="120100" text="市辖区">
<county id="120101" text="和平区"></county>
<county id="120102" text="河东区"></county>
<county id="120103" text="河西区"></county>
</city>
<city id="120200" text="县">
<county id="120221" text="宁河县"></county>
<county id="120223" text="静海县"></county>
<county id="120225" text="蓟县"></county>
</city>
</prov>
因为这个例子的xml只有少量信息,所以不进行加载与否的判断都可以正常运行.而实际上全国的省市很多所以文件很大,不加判断是会出错的.所以希望大虾能帮我彻底解决. 展开
将格式变为:北京市 市辖区 宣武区 这样联动,请问如何修改呢?
3j.html
<script language="JavaScript">
function ChooseState()
{
var source;
var sourceName = "3j.xml";
var source = new ActiveXObject('Microsoft.XMLDOM');
source.async = false;
source.load(sourceName); //装入XML文档
root = source.documentElement; //设置文档元素为根节点元素
sortField=root.selectNodes("//@name"); //搜索属性中含有name的所有节点
for(var i=0;i<sortField.length;++i) //增加省份名称到下拉列表
{
var oOption = document.createElement('OPTION');
oOption.text = " "+sortField[i].text+" ";
oOption.value = sortField[i].text;
form1.SelState.options.add(oOption);
}
ChooseCity();
}
//自定义函数:ChooseCity
//(根据当前选定的省名来读取XML数据中的对应城市名称,并增加到SelCity 的下拉列表中)
function ChooseCity()
{
x=form1.SelState.selectedIndex; //读取省份下拉框的当前选项
y=form1.SelState.options[x].value;
sortField=root.selectNodes("//State[@name='"+y+"']/City"); //搜索name属性值等于参数y的State节点下的所有city节点
for(var i=form1.SelCity.options.length-1;i>=0;--i) //撤消原来的列表项
{
form1.SelCity.options.remove(i)
}
for(var i=0;i<sortField.length;++i) //增加城市名称到下拉列表
{
var oOption = document.createElement('OPTION');
oOption.text = " "+sortField[i].text+" ";
oOption.value = sortField[i].text;
form1.SelCity.options.add(oOption);
}
}</script>
<BODY onLoad="ChooseState()">
<FORM action="" method="post" id="form1" name="form1">
<SELECT name="SelState" id="SelState" onchange="ChooseCity()" >
</SELECT>
<SELECT name="SelCity" id="SelCity" >
</SELECT>
</FORM>
</BODY>
3j.xml
<?xml version="1.0" encoding="GB2312" ?>
<district>
<prov id="110000" text="北京市">
<city id="110100" text="市辖区">
<county id="110101" text="东城区"></county>
</city>
<city id="110200" text="县">
<county id="110228" text="密云县"></county>
<county id="110229" text="延庆县"></county>
</city>
</prov>
<prov id="120000" text="天津市">
<city id="120100" text="市辖区">
<county id="120101" text="和平区"></county>
<county id="120102" text="河东区"></county>
<county id="120103" text="河西区"></county>
</city>
<city id="120200" text="县">
<county id="120221" text="宁河县"></county>
<county id="120223" text="静海县"></county>
<county id="120225" text="蓟县"></county>
</city>
</prov>
因为这个例子的xml只有少量信息,所以不进行加载与否的判断都可以正常运行.而实际上全国的省市很多所以文件很大,不加判断是会出错的.所以希望大虾能帮我彻底解决. 展开
3个回答
展开全部
xml文件,我改了大小写,你可以改回去的。
<?xml version="1.0" encoding="GB2312" ?>
<order>
<prov id="110000" text="北京市">
<city id="110100" text="市辖区">
<county id="110101" text="东城区"></county>
</city>
<city id="110200" text="县">
<county id="110228" text="密云县"></county>
<county id="110229" text="延庆县"></county>
</city>
</prov>
<prov id="120000" text="天津市">
<city id="120100" text="市辖区">
<county id="120101" text="和平区"></county>
<county id="120102" text="河东区"></county>
<county id="120103" text="河西区"></county>
</city>
<city id="120200" text="县">
<county id="120221" text="宁河县"></county>
<county id="120223" text="静海县"></county>
<county id="120225" text="蓟县"></county>
</city>
</prov>
</order>
html文件如下,已经测试通过。
<script type="text/javascript">
var orderDoc = new ActiveXObject("MSXML2.DOMDocument.3.0");
orderDoc.load("3j.xml");
var sortField=orderDoc.selectNodes("/order/prov");
function ChooseState() {
//搜索属性中含有name的所有节点
for(var i=0;i<sortField.length;i++) {
var oOption = document.createElement('OPTION');
var prov=sortField[i].getAttribute('text');
oOption.text = " "+prov+" ";
oOption.value = prov;
form1.SelState.options.add(oOption);
}
ChooseCity();
}
//自定义函数:ChooseCity
//(根据当前选定的省名来读取XML数据中的对应城市名称,并增加到SelCity 的下拉列表中)
function ChooseCity() {
var index=form1.SelState.selectedIndex;
form1.SelCity.length=0;
var selectedCity=sortField[index];
for(var i=0;i<selectedCity.childNodes.length;i++){
var oOption = document.createElement('OPTION');
var city=selectedCity.childNodes[i].getAttribute('text');
oOption.text = " "+city+" ";
oOption.value = city;
form1.SelCity.options.add(oOption);
ChooseCounty();
}
}
function ChooseCounty() {
var indexState=form1.SelState.selectedIndex; //读取省份下拉框的当前选项
var indexCity=form1.SelCity.selectedIndex;
form1.SelCounty.length=0;
var selectedCity=sortField[indexState];
var selectedCounty=selectedCity.childNodes[indexCity];
for(var i=0;i<selectedCounty.childNodes.length;i++){
var oOption = document.createElement('OPTION');
var county=selectedCounty.childNodes[i].getAttribute('text');
oOption.text = " "+county+" ";
oOption.value = county;
form1.SelCounty.options.add(oOption);
}
}
</script>
<BODY onLoad="ChooseState()">
<FORM action="" method="post" id="form1" name="form1">
<SELECT name="SelState" id="SelState" onchange="ChooseCity()" >
</SELECT>
<SELECT name="SelCity" id="SelCity" onchange="ChooseCounty()">
</SELECT>
<SELECT name="SelCounty" id="SelCounty" >
</SELECT>
</FORM>
</BODY>
</html>
最后请楼主旅行诺言,给我200分为盼
-----------不明白你加orderDoc.readyState干什么,好象用xmlrequest才需要判定时间的。
解析xml需要时间的。你可以这样改就可以了,不过本题可以不判定。
将if换成while就可以了
while(orderDoc.readyState==4) ,ok?
<?xml version="1.0" encoding="GB2312" ?>
<order>
<prov id="110000" text="北京市">
<city id="110100" text="市辖区">
<county id="110101" text="东城区"></county>
</city>
<city id="110200" text="县">
<county id="110228" text="密云县"></county>
<county id="110229" text="延庆县"></county>
</city>
</prov>
<prov id="120000" text="天津市">
<city id="120100" text="市辖区">
<county id="120101" text="和平区"></county>
<county id="120102" text="河东区"></county>
<county id="120103" text="河西区"></county>
</city>
<city id="120200" text="县">
<county id="120221" text="宁河县"></county>
<county id="120223" text="静海县"></county>
<county id="120225" text="蓟县"></county>
</city>
</prov>
</order>
html文件如下,已经测试通过。
<script type="text/javascript">
var orderDoc = new ActiveXObject("MSXML2.DOMDocument.3.0");
orderDoc.load("3j.xml");
var sortField=orderDoc.selectNodes("/order/prov");
function ChooseState() {
//搜索属性中含有name的所有节点
for(var i=0;i<sortField.length;i++) {
var oOption = document.createElement('OPTION');
var prov=sortField[i].getAttribute('text');
oOption.text = " "+prov+" ";
oOption.value = prov;
form1.SelState.options.add(oOption);
}
ChooseCity();
}
//自定义函数:ChooseCity
//(根据当前选定的省名来读取XML数据中的对应城市名称,并增加到SelCity 的下拉列表中)
function ChooseCity() {
var index=form1.SelState.selectedIndex;
form1.SelCity.length=0;
var selectedCity=sortField[index];
for(var i=0;i<selectedCity.childNodes.length;i++){
var oOption = document.createElement('OPTION');
var city=selectedCity.childNodes[i].getAttribute('text');
oOption.text = " "+city+" ";
oOption.value = city;
form1.SelCity.options.add(oOption);
ChooseCounty();
}
}
function ChooseCounty() {
var indexState=form1.SelState.selectedIndex; //读取省份下拉框的当前选项
var indexCity=form1.SelCity.selectedIndex;
form1.SelCounty.length=0;
var selectedCity=sortField[indexState];
var selectedCounty=selectedCity.childNodes[indexCity];
for(var i=0;i<selectedCounty.childNodes.length;i++){
var oOption = document.createElement('OPTION');
var county=selectedCounty.childNodes[i].getAttribute('text');
oOption.text = " "+county+" ";
oOption.value = county;
form1.SelCounty.options.add(oOption);
}
}
</script>
<BODY onLoad="ChooseState()">
<FORM action="" method="post" id="form1" name="form1">
<SELECT name="SelState" id="SelState" onchange="ChooseCity()" >
</SELECT>
<SELECT name="SelCity" id="SelCity" onchange="ChooseCounty()">
</SELECT>
<SELECT name="SelCounty" id="SelCounty" >
</SELECT>
</FORM>
</BODY>
</html>
最后请楼主旅行诺言,给我200分为盼
-----------不明白你加orderDoc.readyState干什么,好象用xmlrequest才需要判定时间的。
解析xml需要时间的。你可以这样改就可以了,不过本题可以不判定。
将if换成while就可以了
while(orderDoc.readyState==4) ,ok?
展开全部
<?xml version="1.0" encoding="GB2312" ?>
<order>
<prov id="110000" text="北京市">
<city id="110100" text="市辖区">
<county id="110101" text="东城区"></county>
</city>
<city id="110200" text="县">
<county id="110228" text="密云县"></county>
<county id="110229" text="延庆县"></county>
</city>
</prov>
<prov id="120000" text="天津市">
<city id="120100" text="市辖区">
<county id="120101" text="和平区"></county>
<county id="120102" text="河东区"></county>
<county id="120103" text="河西区"></county>
</city>
<city id="120200" text="县">
<county id="120221" text="宁河县"></county>
<county id="120223" text="静海县"></county>
<county id="120225" text="蓟县"></county>
</city>
</prov>
</order>
html文件如下,已经测试通过。
<script type="text/javascript">
var orderDoc = new ActiveXObject("MSXML2.DOMDocument.3.0");
orderDoc.load("3j.xml");
var sortField=orderDoc.selectNodes("/order/prov");
function ChooseState() {
//搜索属性中含有name的所有节点
for(var i=0;i<sortField.length;i++) {
var oOption = document.createElement('OPTION');
var prov=sortField[i].getAttribute('text');
oOption.text = " "+prov+" ";
oOption.value = prov;
form1.SelState.options.add(oOption);
}
ChooseCity();
}
//自定义函数:ChooseCity
//(根据当前选定的省名来读取XML数据中的对应城市名称,并增加到SelCity 的下拉列表中)
function ChooseCity() {
var index=form1.SelState.selectedIndex;
form1.SelCity.length=0;
var selectedCity=sortField[index];
for(var i=0;i<selectedCity.childNodes.length;i++){
var oOption = document.createElement('OPTION');
var city=selectedCity.childNodes[i].getAttribute('text');
oOption.text = " "+city+" ";
oOption.value = city;
form1.SelCity.options.add(oOption);
ChooseCounty();
}
}
function ChooseCounty() {
var indexState=form1.SelState.selectedIndex; //读取省份下拉框的当前选项
var indexCity=form1.SelCity.selectedIndex;
form1.SelCounty.length=0;
var selectedCity=sortField[indexState];
var selectedCounty=selectedCity.childNodes[indexCity];
for(var i=0;i<selectedCounty.childNodes.length;i++){
var oOption = document.createElement('OPTION');
var county=selectedCounty.childNodes[i].getAttribute('text');
oOption.text = " "+county+" ";
oOption.value = county;
form1.SelCounty.options.add(oOption);
}
}
</script>
<BODY onLoad="ChooseState()">
<FORM action="" method="post" id="form1" name="form1">
<SELECT name="SelState" id="SelState" onchange="ChooseCity()" >
</SELECT>
<SELECT name="SelCity" id="SelCity" onchange="ChooseCounty()">
</SELECT>
<SELECT name="SelCounty" id="SelCounty" >
</SELECT>
</FORM>
</BODY>
</html>
<order>
<prov id="110000" text="北京市">
<city id="110100" text="市辖区">
<county id="110101" text="东城区"></county>
</city>
<city id="110200" text="县">
<county id="110228" text="密云县"></county>
<county id="110229" text="延庆县"></county>
</city>
</prov>
<prov id="120000" text="天津市">
<city id="120100" text="市辖区">
<county id="120101" text="和平区"></county>
<county id="120102" text="河东区"></county>
<county id="120103" text="河西区"></county>
</city>
<city id="120200" text="县">
<county id="120221" text="宁河县"></county>
<county id="120223" text="静海县"></county>
<county id="120225" text="蓟县"></county>
</city>
</prov>
</order>
html文件如下,已经测试通过。
<script type="text/javascript">
var orderDoc = new ActiveXObject("MSXML2.DOMDocument.3.0");
orderDoc.load("3j.xml");
var sortField=orderDoc.selectNodes("/order/prov");
function ChooseState() {
//搜索属性中含有name的所有节点
for(var i=0;i<sortField.length;i++) {
var oOption = document.createElement('OPTION');
var prov=sortField[i].getAttribute('text');
oOption.text = " "+prov+" ";
oOption.value = prov;
form1.SelState.options.add(oOption);
}
ChooseCity();
}
//自定义函数:ChooseCity
//(根据当前选定的省名来读取XML数据中的对应城市名称,并增加到SelCity 的下拉列表中)
function ChooseCity() {
var index=form1.SelState.selectedIndex;
form1.SelCity.length=0;
var selectedCity=sortField[index];
for(var i=0;i<selectedCity.childNodes.length;i++){
var oOption = document.createElement('OPTION');
var city=selectedCity.childNodes[i].getAttribute('text');
oOption.text = " "+city+" ";
oOption.value = city;
form1.SelCity.options.add(oOption);
ChooseCounty();
}
}
function ChooseCounty() {
var indexState=form1.SelState.selectedIndex; //读取省份下拉框的当前选项
var indexCity=form1.SelCity.selectedIndex;
form1.SelCounty.length=0;
var selectedCity=sortField[indexState];
var selectedCounty=selectedCity.childNodes[indexCity];
for(var i=0;i<selectedCounty.childNodes.length;i++){
var oOption = document.createElement('OPTION');
var county=selectedCounty.childNodes[i].getAttribute('text');
oOption.text = " "+county+" ";
oOption.value = county;
form1.SelCounty.options.add(oOption);
}
}
</script>
<BODY onLoad="ChooseState()">
<FORM action="" method="post" id="form1" name="form1">
<SELECT name="SelState" id="SelState" onchange="ChooseCity()" >
</SELECT>
<SELECT name="SelCity" id="SelCity" onchange="ChooseCounty()">
</SELECT>
<SELECT name="SelCounty" id="SelCounty" >
</SELECT>
</FORM>
</BODY>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
修改:<SELECT name="SelCity" id="SelCity" onchange="ChooseCounty()">
</SELECT>
增加::<SELECT name="SelCounty" id="SelCounty" >
</SELECT>
修改:<BODY onLoad="ChooseState();ChooseCounty()">
增加:
//自定义函数:ChooseCounty
function ChooseCounty()
{
x=form1.SelCity.selectedIndex; //读取省份下拉框的当前选项
y=form1.SelCity.options[x].value;
sortField=root.selectNodes("//City[@name='"+y+"']/County"); //搜索name属性值等于参数y的State节点下的所有city节点
for(var i=form1.SelCounty.options.length-1;i>=0;--i) //撤消原来的列表项
{
form1.SelCounty.options.remove(i)
}
for(var i=0;i<sortField.length;++i) //增加城市名称到下拉列表
{
var oOption = document.createElement('OPTION');
oOption.text = " "+sortField[i].text+" ";
oOption.value = sortField[i].text;
form1.SelCounty.options.add(oOption);
}
}
思想大概是这样,只是不知道root.selectNodes("//City[@name='"+y+"']/County"); 取值是否是这样取
</SELECT>
增加::<SELECT name="SelCounty" id="SelCounty" >
</SELECT>
修改:<BODY onLoad="ChooseState();ChooseCounty()">
增加:
//自定义函数:ChooseCounty
function ChooseCounty()
{
x=form1.SelCity.selectedIndex; //读取省份下拉框的当前选项
y=form1.SelCity.options[x].value;
sortField=root.selectNodes("//City[@name='"+y+"']/County"); //搜索name属性值等于参数y的State节点下的所有city节点
for(var i=form1.SelCounty.options.length-1;i>=0;--i) //撤消原来的列表项
{
form1.SelCounty.options.remove(i)
}
for(var i=0;i<sortField.length;++i) //增加城市名称到下拉列表
{
var oOption = document.createElement('OPTION');
oOption.text = " "+sortField[i].text+" ";
oOption.value = sortField[i].text;
form1.SelCounty.options.add(oOption);
}
}
思想大概是这样,只是不知道root.selectNodes("//City[@name='"+y+"']/County"); 取值是否是这样取
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询