【发布时间】:2011-09-13 18:47:41
【问题描述】:
我正在玩 jquery ui 自动完成。还有一个关于如何查询 XML 数据的问题。我有一个包含位置列表的 XML 文件,类似于:
<geoname>
<name_en>The Tower of London</name_en>
<name_fr>Example text</name_fr>
<lat>51.5082349601834</lat>
<lng>-0.0763034820556641</lng>
<geonameId>6286786</geonameId>
<countryCode>GB</countryCode>
<countryName>United Kingdom</countryName>
<fcl>S</fcl>
<fcode>CSTL</fcode>
<web>http://www.exmaple.com</web>
</geoname>
我的 jQuery 是:
jQuery(document).ready(function() {
lang = 'en';
$.ajax({
url: "places.xml",
dataType: "xml",
success: function( xmlResponse ) {
var data = $( "countryCode", xmlResponse ).map(function() {
return {
value: $( "name", this ).text(),
id: $( "geonameId", this ).text(),
countryName: $( "countryName", this ).text(),
link: $( "web", this ).text(),
code: $( "countryCode", this ).text()
};
}).get();
$( "#results" ).autocomplete({
source: data,
minLength: 0,
select: function( event, ui ) {
$('#foo').html('');
$('#foo').html(ui.item.code).slideDown();
}
});
}
});
});
我遇到的问题是,我想指定一个变量,当我将其设置为 EN 时仅搜索 name_en,而在其他情况下,仅在设置为 FR 时搜索 name_fr。当我将语言设置为 EN 时,我不希望 name_fr 结果返回。提前致谢。
【问题讨论】:
-
return { value: $( "name_fr", this ).text(),对不起,现在知道了。 :)
标签: jquery xml jquery-ui search jquery-autocomplete