Google Maps API:在提交之前,如何确保Google Maps自动填充文字是真实地址? [英] Google Maps API: How do you ensure that the Google Maps Autocomplete text is an actual address before submitting?

查看:84
本文介绍了Google Maps API:在提交之前,如何确保Google Maps自动填充文字是真实地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用自动完成功能的Google Maps搜索框(当用户在字段中输入文本时,将显示地址建议的下拉列表.)

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 Maps地址),并建立了一个受

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 Maps建议
  • 触发事件以将注意力集中在第一个建议上
  • 触发事件以选择它

我复制自己的代码的结果(使用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自动填充文字是真实地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆