我正在尝试创建一个实现我创建的XML文件的自动完成搜索栏。我正在关注jQuery上的XML演示。我的脚本如下:
$(function() {
function log( message ) {
$( "<div/>" ).text( message ).prependTo( "#log" );
$( "#log" ).attr( "scrollTop", 0 );
}
$.ajax({
url: "courses.xml",
dataType: "xml",
success: function( xmlResponse ) {
var data = $( "course", xmlResponse ).map(function() {
return {
value: $( "department_name", this ).text() + ", " +
( $.trim( $( "class_number", this ).text() ) || "(unknown course)" ),
id: $( "id", this ).text()
};
}).get();
$( "#results" ).autocomplete({
source: data,
minLength: 0,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.value + ", ID: " + ui.item.id :
"Nothing selected, input was " + this.value );
}
});
}
});
});
XML文件具有以下格式:
<courses>
<course id="1">
<department_name>ELEC</department_name>
<class_number>2001</class_number>
<section_number>001</section_number>
<professor>Chris, Michael</professor>
<class_title>Electrical Engineering Concepts</class_title>
<time_of_day>14:00-15:15</time_of_day>
<days_of_week>TuTh</days_of_week>
<building>Randalph Hall</building>
</course>
搜索栏没有返回任何内容。没有自动完成功能。
感谢所有帮助