我有一個下拉菜單,我需要從XML文件填充。 這裏是我嘗試使用該腳本:使用JQuery/AJAX來填充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 並有類似的結果,沒有錯誤,但沒有數據。
在此先感謝您的幫助。
謝謝!就是這樣。 – user2087788 2013-02-19 21:12:53
如果格式化爲xml,我可以使用txt文件嗎?我需要提取數據的報告來自另一個系統,當我們保存爲xml時,我們會得到額外的字符。我試着將數據類型改爲txt,但是我得到了loadfail錯誤。 – user2087788 2013-02-20 17:07:53
將數據類型保留爲xml,但根據需要更改url參數以獲取所需的文件。那樣有用嗎? – Luis 2013-02-24 06:14:55