需要帮助通过javascript脚本循环浏览xml文件
问题描述:
这是我第一次以任何方式使用ajax,请耐心等待。需要帮助通过javascript脚本循环浏览xml文件
在我的程序中,我有一个下拉框,根据所选的状态动态地提取邮政编码,县和城市。到目前为止,各县工作正常,但邮政编码和城市只显示我的XML表单上的第一条记录。
的XML看起来是这样的:
<states>
<counties>
<county>
<countyid>id#1</countyid>
<countyname>nassau</countyname>
</county>
</counties>
<zipcodes>
<zip>10109</zip>
</zipcodes>
<cities>
<city>New York</city>
<cities>
</states>
现在,通过县部分循环的JavaScript看起来像这样:
target1.options[0] = new Option("Select County", "null");
for (var i = 0; i < xmlCounties.length; i++) {
target1.options[target1.options.length] = new Option(xmlCounties[i].childNodes[1].firstChild.nodeValue, xmlCounties[i].childNodes[0].firstChild.nodeValue, false, (matched == xmlCounties[i].childNodes[0].firstChild.nodeValue));
}
这工作得很好,但对于城市和邮政编码他们没有,它们都是这样的,并且与上述示例相同:
target2.options[0] = new Option("Select Zipcode", "null");
for (var i = 0; i < xmlZips.length; i++) {target2.options[target2.options.length] = new Option(xmlZips[i].childNodes[0].firstChild.nodeValue, xmlZips[i].childNodes[0].firstChild.nodeValue, false, (matched == xmlZips[i].childNodes[0].firstChild.nodeValue));
}
它们都从xml中提取数据,但只是第一条记录。任何想法如何得到这个固定?谢谢!
答
县是2级深..拉链和城市只有一个层次深。那就是原因。试试这个
target2.options[0] = new Option("Select Zipcode", "null");
for (var i = 0; i < xmlZips.length; i++) {target2.options[target2.options.length] = new Option(xmlZips[i].childNodes[0].nodeValue, xmlZips[i].childNodes[0].nodeValue, false, (matched == xmlZips[i].childNodes[0].nodeValue));
}
+0
我不认为这是因为我在我的XML中为邮政编码和城市获取第一条记录,它只是没有循环并拉动所有的结果。我试过你的建议,它只是拉空值。我很欣赏这个建议,谢谢。 – rshivers 2011-05-12 19:52:48
你使用哪个JavaScript库(如果有的话)? – jimbojw 2011-05-12 19:31:16
我没有使用任何库,只是直起来的JavaScript。 – rshivers 2011-05-12 19:39:10