[英]Using javascript and XML to populate a drop-down box
所有這一切都非常新,請耐心等待,並使用簡單的答案!
我試圖動態填充一個下拉列表框(theSecondBox),其中包含一個項目列表,該列表取決於在另一個下拉框(theCriteria)中所做的選擇。
我已經能夠發出AJAX請求,並且正在獲取XML文檔。
到目前為止,我的腳本看起來像這樣:
<script type="text/javascript">
var xmlHttp;
function triggerAction(){
xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
criteria = document.getElementById("theCriteria").value;
xmlHttp.open("GET","MyURL?criteria=" + criteria, true);
xmlHttp.send(null);
}
function handleStateChange(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status == 200){
parseResults();}
}
}
這會向我返回一個如下所示的XML文檔:
<list>
<string>FirstDropDownItem</string>
<string>SecondDropDownItem</string>
<string>ThirdDropDownItem</string>
</list>
因此,我正在嘗試使用類似...
function parseResults(){
var results
var selectTag
results = xmlHttp.responseXML;
selectTag = document.getElementById("theSecondBox");
...這是我需要一些“簡單”指針的地方。
如何用XML文檔中返回的項目填充secondBox? 到目前為止,我嘗試過的所有操作(為了避免使我尷尬,我都不會復制它!)只會讓我感到無聊。
對於這個簡單的問題表示歉意,但是正如我所說,無論如何我都不是程序員,因此,任何對解決方案或有助於教育我的良好教程的指導都將不勝感激。
w3schools不是一個很好的來源,但是這一次請看一下(現在IE5的“如果”已過時): http : //www.w3schools.com/ajax/tryit.asp? filename=tryajax_first
坦率地說,document.getElementById(“ myDiv”)。innerHTML = xmlhttp.responseText是您所需要的。 在此之前,您可以將“ list”替換為“ ul”,將字符串替換為“ li”。
最好在后端更改xml,但是如果您不能嘗試str.replace ...
var new_text = text.replace(/ want / g,“不想要”);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.