我有兴趣实现一个旁边带有“提交”/“开始”按钮的 Google map 搜索按钮(例如在 http://maps.google.com 上)。如果我在搜索框中按 Enter,一切都很好,但我不知道如何使用按钮强制/提交搜索。
现在我的代码基于以下示例:https://developers.google.com/maps/documentation/javascript/examples/places-searchbox .最重要的部分是我现在正在使用它:
HTML:
<div id="intro-search">
<input id="search-box" />
<button type="button">Submit!</button>
</div>
JS:
// Listen for the event fired when the user selects an item from the
// pick list. Retrieve the matching places for that item.
google.maps.event.addListener(searchBox, 'places_changed', function() {
// ...
// Show the results on the map
// ...
}
我不知道如何在单击按钮时触发同样的事情。
另一方面,SearchBox 之间有什么区别?和 Autocomplete控制?他们不做同样的事情吗?
最佳答案
对于搜索框,你必须像这样在输入字段上使用谷歌地图的触发事件
document.getElementById('my-button').onclick = function () {
var input = document.getElementById('my-input');
google.maps.event.trigger(input, 'focus', {});
google.maps.event.trigger(input, 'keydown', { keyCode: 13 });
google.maps.event.trigger(this, 'focus', {});
};
关于javascript - 谷歌地图 - 点击按钮触发搜索框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20407045/