我正在使用 Google Places Autocomplete,我只是希望它在表单字段中按下回车键并且存在建议时选择结果列表中的顶部项目。我知道以前有人问过这个问题:
Google 地图 Places API V3 自动完成 - 输入时选择第一个选项
Google maps Places API V3 自动完成 - 在输入时选择第一个选项(并保持这种状态)
但是这些问题的答案似乎并没有真正起作用,或者它们解决了特定的附加功能。
看起来像下面这样的东西应该工作(但它没有):
$("input#autocomplete").keydown(function(e) {
if (e.which == 13) {
//if there are suggestions...
if ($(".pac-container .pac-item").length) {
//click on the first item in the list or simulate a down arrow key event
//it does get this far, but I can't find a way to actually select the item
$(".pac-container .pac-item:first").click();
} else {
//there are no suggestions
}
}
});
任何建议将不胜感激!