我目前正在使用单个 INPUT 字段来触发 jQuery UI Autocomplete,它从 Geonames 中提取数据。
$( "#city_state_country" ).autocomplete({
source: function( request, response ) {
$.ajax({
url: "http://ws.geonames.org/searchJSON",
dataType: "jsonp",
data: {
featureClass: "P",
style: "full",
maxRows: 6,
name_startsWith: request.term
},
success: function( data ) {
response( $.map( data.geonames, function( item ) {
return {
label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
value: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
}
}));
}
});
},
minLength: 3,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.label :
"Nothing selected, input was " + this.value);
},
open: function() {
$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
},
close: function() {
$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
}
});
所以如果我开始输入“Bosto”
它为我提供
Boston, Massachusetts, United States
但我想为城市、州和国家提供单独的 INPUT 字段——并且它们每个都具有与 Geonames 挂钩的自动完成功能,分别只返回对城市、州、国家的建议。
所以输入:
"Bost" in the city INPUT would provide "Boston".
"Massa" in the state INPUT would provide "Massachusetts".
"United St" in the state INPUT would provide "United States".
这可能吗?
我已经尝试过更改name_startsWith
为其他选项,但没有达到我的预期。