我正在使用Google Maps Places V3自动完成。我希望有一个功能,当用户开始在搜索框中输入时,自动选择自动完成下拉菜单中的第一项。
类似于Facebook的搜索功能。
我已经借助这两个线程更新了Google地图自动完成: Google Autocomplete - enter to select Google maps Places API V3 autocomplete - select first option on enter 但是我找不到解决这个新问题的方法...
我在这里发布了我的代码: http://jsfiddle.net/Chazz09/YfPv3/5/
类似于Facebook的搜索功能。
我已经借助这两个线程更新了Google地图自动完成: Google Autocomplete - enter to select Google maps Places API V3 autocomplete - select first option on enter 但是我找不到解决这个新问题的方法...
我在这里发布了我的代码: http://jsfiddle.net/Chazz09/YfPv3/5/
$(document).ready(function(){
var pac_input = document.getElementById('searchfield');
// prevents enter key to submit form//
$('#searchfield').keydown(function (e) {
if (e.which == 13 && $('.pac-container:visible').length) return false;
});
// prevents enter key to submit form//
(function pacSelectFirst(input){
// store the original event binding function
var _addEventListener = (input.addEventListener) ? input.addEventListener : input.attachEvent;
function addEventListenerWrapper(type, listener) {
// Simulate a 'down arrow' keypress on hitting 'return' when no pac suggestion is selected,
// and then trigger the original listener.
if (type == "keydown") {
var orig_listener = listener;
listener = function (event) {
var suggestion_selected = $(".pac-item.pac-selected").length > 0;
if (event.which == 13 && !suggestion_selected) {
var simulated_downarrow = $.Event("keydown", {keyCode:40, which:40})
orig_listener.apply(input, [simulated_downarrow]);
}
orig_listener.apply(input, [event]);
};
}
// add the modified listener
_addEventListener.apply(input, [type, listener]);
}
if (input.addEventListener)
input.addEventListener = addEventListenerWrapper;
else if (input.attachEvent)
input.attachEvent = addEventListenerWrapper;
})(pac_input);
$(document).ready(function()
{
function initialize() {
var options = {
types: ['geocode'],
componentRestrictions: {country: "fr"}
};
var autocomplete = new google.maps.places.Autocomplete(pac_input, options);
}
google.maps.event.addDomListener(window, 'load', initialize);
});
});