我有一個問題,我的代碼讀取xml.I有使用AJAX來讀取XML數據,並在組合框中填充它。我的問題是,它只能讀取第一data.Here是我的代碼
我這樣的XML
<info>
<area>
<code>1</code>
<name>area1</name>
</area>
<area>
<code>2</code>
<name>area2</name>
</area>
</info>
和我的javascript
if(http.readyState == 4 && http.status == 200) {
//get select elements
var item = document.ProblemMaintenanceForm.elements["probArea"];
//empty combobox
item.options.length = 0;
//read xml data from action file
var test = http.responseXML.getElementsByTagName("area");
alert(test.length);
for (var i=0; i < test.length; i++){
var tests = test[i];
item.options[item.options.length] = new Option(tests.getElementsByTagName("name")[i].childNodes[0].nodeValue,tests.getElementsByTagName("code")[i].childNodes[0].nodeValue);
}
}
你有過在調試代碼加強,以確認你回來了XML是什麼你期望?通過它,看看會發生什麼。您可以使用IE8的開發人員工具或Firefox的Firebug插件逐行跟蹤執行情況。檢查你的數組的值,並確保你爲每個屬性使用正確的索引。乍一看,它看起來像childNodes [i]是不正確的。 – RMorrisey 2010-05-15 03:10:36
我有一步一步通過代碼和XML是相同的,我真的希望 – Najmi 2010-05-15 04:41:24
請使用JSON而不是XML,如果可以的話,這是一個更好的格式這種有點東西。 – vsync 2010-05-15 08:27:17