我有一个 jQuery 自动完成字段,当用户开始在该字段中输入城市名称时,它会查找城市名称和相应的 ZIP(美国邮政编码)。
MySQL 数据库包含大约 44,000 行美国邮政编码。选择所有城市名称和邮政编码时,查询本身运行速度很快(0.0002 秒)。
“预缓存”所有结果是否有任何好处 (SELECT place_name
, postal_code
FROM postal_codes
WHERE country_code
= 'US') 以便用户更快地加载自动完成功能?
编辑:整个 JSON 列表为 600KB,并在浏览器中加载 2-3 秒(根据 Google Chrome 控制台)
当前代码:
<script type="text/javascript">
$(function() {
$('#from_country').change(function(){
var val = this.value;
$("#from_zip").autocomplete({
source: "json-list-live-production.php?country=" + val,
minLength: 4,
});
}).change()
$('#del_country').change(function(){
var val = this.value;
$("#del_zip").autocomplete({
source: "json-list-live-production.php?country=" + val,
minLength: 4,
});
}).change()
});
</script>
<input type="text" class="input_text" name="from_zip" id="from_zip" />
<input type="text" name="del_zip" id="del_zip" />
最佳答案
看起来使用 GeoNames.org 网络服务 + jQuery 自动完成查找邮政编码是最快的方法。
$( "#from_zip" ).autocomplete({
source: function( request, response ) {
$.ajax({
url: "http://ws.geonames.org/postalCodeSearchJSON",
dataType: "jsonp",
data: {
featureClass: "P",
style: "full",
maxRows: 50,
placename_startsWith: request.term,
country: "US"
},
success: function( data ) {
response( $.map( data.postalCodes, function( item ) {
return {
label: item.placeName + (item.adminCode1 ? ", " + item.adminCode1 : "") + " (" + item.postalCode + ")",
value: item.postalCode
}
}));
}
});
},
minLength: 2,
messages: {
noResults: null,
results: function() {}
},
});
关于mysql - jQuery 自动完成 : pre-cache query to increase performance?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15791518/