问题描述
多年来,我一直在使用以下脚本:https://github.com/stephjang/placecomplete,没有任何问题。今晚,我收到许多投诉,抱怨用户无法从下拉菜单中选择地址。键入地址可以很好地工作,并且会显示结果,但是当尝试选择一个地址时,它不会执行任何操作。有人知道Google阻止placecomplete请求是否发生了变化?
我的代码:
@H_502_5@$inputLocationSearch.placecomplete({ placeholderText: "Enter your site location...",requestParams: { componentRestrictions: {country: ['ca','us']} } });
问题的屏幕截图(鼠标或键盘无法选择找到的地址):
解决方法
在插件jquery.placecomplete.js
代码中发现了问题。替换了默认的id
参数以使用place_id
,因为Google自动完成功能已弃用id
。
我在现有代码apr["id"] = apr["place_id"];
上方添加了apr["text"] = apr["description"];
下面是整个代码块:
var select2options = $.extend({},{
query: function(query) {
GooglePlacesAPI.getPredictions(query.term,requestParams)
.done(function(aprs) {
var results = $.map(aprs,function(apr) {
// Select2 needs a "text" and "id" property set
// for each autocomplete list item. "id" is
// already defined on the apr object
apr["id"] = apr["place_id"];
apr["text"] = apr["description"];
return apr;
});
query.callback({results: results});
})
.fail(function(errorMsg) {
$el.trigger(pluginName + ":error",errorMsg);
query.callback({results: []});
});
},initSelection: function(element,callback) {
// initSelection() was triggered by value being defined directly
// in the input element HTML
var initText = $el.val();
// The id doesn't matter here since we're just trying to prefill
// the input with text for the user to see.
callback({id: 0,text: initText});
},minimumInputLength: 1,selectOnBlur: true,allowClear: true,multiple: false,dropdownCssClass: "jquery-placecomplete-google-attribution",placeholder: this.options.placeholderText
},this.options);