【发布时间】:2019-02-25 20:27:18
【问题描述】:
我有一个需要从 XML 文件填充的下拉菜单。 这是我尝试使用的脚本:
$(document).ready(function(){ // load jQuery 1.5
function loadfail(){
alert("Error: Failed to read file!");
}
function parse(document){
$(document).find("menuItem").each(function(){
var optionLabel = $(this).find('text').text();
var optionValue = $(this).find('value').text();
$('.opening').append(
'<option value="'+ optionValue + '">' + optionLabel + '</option>'
);
});
}
$.ajax({
url: 'http://ourwebserver/Online%20App/jobTitles.xml', // name of file with our data - link has been renamed
dataType: 'xml', // type of file we will be reading
success: parse, // name of function to call when done reading file
error: loadfail // name of function to call when failed to read
});
});
这是 xml 文件中的一个示例:
<menu>
<menuItem>
<value>612</value>
<text>CLERK-CMH HOS HIM</text>
</menuItem>
<menuItem>
<value>1632</value>
<text>FAM PRACT-CMH CLN Southside Medical</text>
</menuItem>
这是包含我要填充的下拉列表的 html 代码:
<strong>Position(s) Desired</strong>
<select name="opening" size="5" multiple="multiple" id="opening">
</select>
我没有收到错误消息,但我也没有收到任何数据来填充菜单。
我也试过这个链接的代码/解决方案: populating a drop down menu with xml file 并且有类似的结果,没有错误,但没有数据。
提前感谢您的帮助。
【问题讨论】: