我正在使用这种方法来加载我的数据:
这是我的代码
<script>
$(function() {
function log( message ) {
$( "<div/>" ).html( message ).prependTo( "#log" );
$( "#log" ).attr( "scrollTop", 0 );
}
$.ajax({
url: "countries.xml",
dataType: "xml",
success: function( xmlResponse ) {
var data = $( "geoname", xmlResponse ).map(function() {
return {
name: ( $.trim( $( "name", this ).text() ) ),
number: ( $.trim( $( "number", this ).text() ) || undefined ),
icon: ( $.trim( $( "icon", this ).text() ) || undefined ),
value: $( "name", this ).text() + " " ,
zip: $( "zip", this ).text()
};
}).get();
console.log(data);
$( "#birds" ).autocomplete({
source: data,
minLength: 0,
select: function( event, ui, icon ) {
console.log(ui.item);
log( ui.item ?
"Selected: " + ui.item.value + ", zip: " + ui.item.zip :
"Nothing selected, input was " + this.value );
$( "#project-icon" ).attr( "src", "images/" + ui.item.icon );
if(ui.item) {
$.ajax({
url: "results.xml",
dataType: "xml",
success: function( xmlResponse ) {
var items = $( "geoname", xmlResponse ).map(function() {
if(ui.item.name == $.trim( $( "country", this ).text() )) {
return {
name: ( $.trim( $( "name", this ).text() ) || undefined ),
country: ( $.trim( $( "country", this ).text() ) || undefined ),
price: ( $.trim( $( "price", this ).text() ) || undefined ),
price2: ( $.trim( $( "price2", this ).text() ) || undefined ),
planprice: ( $.trim( $( "planprice", this ).text() ) || undefined ),
plandisplay: ( $.trim( $( "plandisplay", this ).text() ) || undefined ),
icon: ( $.trim( $( "icon", this ).text() ) || undefined ),
actionurl: ( $.trim( $( "actionurl", this ).text() ) || undefined ),
text: ( $.trim( $( "text", this ).text() ) || undefined ),
value: $( "name", this ).text() + ", " +
( '('+$.trim( $( "zip", this ).text()+')' ) || "(unknown country)" ),
zip: $( "zip", this ).text()
};
}
}).get();
// $( "#log" ).html('');
log('<hr/>');
$.each(items, function(index, obj) {
var str = ('<span style="color:#666;font-weight:bold;font-size:17px;">')+obj.name+('</span>')+":"+obj.price+"c "+('<strong>')+obj.price2+"c (incl.VAT)"+obj.plandisplay+obj.planprice;
log(str);
// console.log(arguments);
});
}
});
} else {
log( "Nothing selected, input was " + this.value );
}
}
});
}
});
});
</script>
<input id="birds" placeholder="Country name" />
<img id="project-icon" src="images/transparent_1x1.png" class="ui-state-default" alt="" />
<div class="ui-widget" style="margin-top: 2em; font-family: Arial;">
<div id="log" style=" overflow: auto; border-width: 0;" class="ui-widget-content"></div>
</div>
如果您每次进行新搜索时检查 jquery 的 XML 示例,旧结果仍然存在,新结果将显示在顶部。
如何清除每次搜索的结果?
每一次搜索都是独一无二的