Google Maps API:在提交之前,您如何确保 Google Maps Autocomplete 文本是真实地址? [英] Google Maps API: How do you ensure that the Google Maps Autocomplete text is an actual address before submitting?
问题描述
我有一个使用自动完成功能的 Google 地图搜索框(当用户在字段中输入文本时,会出现一个地址建议下拉列表).
I have a Google Maps search box using autocomplete (as the user inputs text in the field, a dropdown of address suggestions appears).
我想确保当用户提交表单时,字段内的文本是实际的自动完成建议.
I want to make sure that when a user submits the form, the text inside the field is an actual Autocomplete suggestion.
我的目标是确保输入的文本是实际地址,而不是随机字符串.
My goal is to ensure that the text entered is an actual adress, rather than a random string.
推荐答案
我遇到了类似的问题(给定字段中的任何内容都应该是有效的 Google 地图地址),并从 这个 可能对你有帮助.这个想法是:
I had a similar issue (that anything in the given field should be valid Google Maps address), and built a solution inspired from this one, that may help you. The idea is :
- 当用户执行其他操作而不是点击 Google 地图建议时
- 触发事件以将焦点转移到第一个建议上
- 触发事件以选择它
我为自己的代码复制结果(使用 Jquery):
I copy the result for my own code (with Jquery) :
//// Ensuring that only Google Maps adresses are inputted
function selectFirstAddress (input) {
google.maps.event.trigger(input, 'keydown', {keyCode:40});
google.maps.event.trigger(input, 'keydown', {keyCode:13});
}
// Select first address on focusout
$('#content').on('focusout', 'input#xxx__zzz', function() {
selectFirstAddress(this);
});
// Select first address on enter in input
$('#content').on('keydown', 'input#xxx__zzz', function(e) {
if (e.keyCode == 13) {
selectFirstAddress(this);
}
});
对我来说它有效,我希望它有帮助!
For me it works, I hope it helps !
这篇关于Google Maps API:在提交之前,您如何确保 Google Maps Autocomplete 文本是真实地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!