【问题标题】:How to fire place_changed event for Google places auto-complete on Enter key如何在 Enter 键上触发 Google Places 自动完成的 place_changed 事件
【发布时间】:2012-09-30 18:03:11
【问题描述】:

点击似乎触发了事件并设置了 cookie,但按 Enter 提交并没有设置 cookie,而是页面在没有 cookie 的情况下重定向。

function locationAuto() {
        $('.search-location').focus(function () {
        autocomplete = new google.maps.places.Autocomplete(this);
        searchbox = this;

        google.maps.event.addListener(autocomplete, 'place_changed', function () {
            var thisplace = autocomplete.getPlace();
            if (thisplace.geometry.location != null) {
                $.cookie.raw = true;
                $.cookie('location', searchbox.value, { expires: 1 });
                $.cookie('geo', thisplace.geometry.location, { expires: 1 });
            }
        });
});

.search-location 是多个文本框上的一个类。 有一个提交按钮,可以从 cookie 中获取值并重定向(服务器端)

【问题讨论】:

  • 这不是 Google Mapa API 问题。
  • 它在 Google Maps API v3 Places Library 中
  • 问题是关于在 DOM 元素上触发事件,而不是关于 Google Maps API。

标签: autocomplete google-places


【解决方案1】:

改编自 Jonathan Caulfield 的回答:

$('.search-location').keypress(function(e) {
  if (e.which == 13) {
    google.maps.event.trigger(autocomplete, 'place_changed');
    return false;
  }
});

【讨论】:

  • 不错的答案。我能够轻松地调整它来做我想做的事,即触发自动完成侦听器,然后继续提交表单。
  • 已经有一段时间没有回答这个问题了,所以谷歌的自动完成小部件可能发生了一些变化,但如果我使用此代码,autocomplete 对象上的getPlace() 方法将返回 null。标准的 place_changed 处理程序似乎在按键时被触发,所以从这里返回 false 就足以(对我而言)阻止包含表单被提交。
【解决方案2】:

我也遇到过这个问题,想出了一个好的解决方案。在我的网站中,我想在提交之前将 autocomplete.getPlace().formatted_address 保存在隐藏的输入中。单击表单的提交按钮时,这按预期工作,但在自动完成下拉菜单中的选择上按 Enter 键时却没有。我的解决方案如下:

$(document).ready(function() {

    // Empty the value on page load
    $("#formattedAddress").val("");
    // variable to indicate whether or not enter has been pressed on the input
    var enterPressedInForm = false;

    var input = document.getElementById("inputName");
    var options = {
      componentRestrictions: {country: 'uk'}
    };
    autocomplete = new google.maps.places.Autocomplete(input, options);

    $("#formName").submit(function(e) {
        // Only submit the form if information has been stored in our hidden input
        return $("#formattedAddress").val().length > 0;
    });

    $("#inputName").bind("keypress", function(e) {
        if(e.keyCode == 13) {
            // Note that simply triggering the 'place_changed' event in here would not suffice, as this would just create an object with the name as typed in the input field, and no other information, as that has still not been retrieved at this point.

            // We change this variable to indicate that enter has been pressed in our input field
            enterPressedInForm = true;
        }
    });

    // This event seems to fire twice when pressing enter on a search result. The first time getPlace() is undefined, and the next time it has the data. This is why the following logic has been added.
    google.maps.event.addListener(autocomplete, 'place_changed', function () {
        // If getPlace() is not undefined (so if it exists), store the formatted_address (or whatever data is relevant to you) in the hidden input.
        if(autocomplete.getPlace() !== undefined) {
            $("#formattedAddress").val(autocomplete.getPlace().formatted_address);
        }
        // If enter has been pressed, submit the form.
        if(enterPressedInForm) {
            $("#formName").submit();
        }
    });
});

这个解决方案似乎效果很好。

【讨论】:

  • 评论“在搜索结果上按 Enter 时,此事件似乎触发了两次。第一次 getPlace() 未定义”救了我。我所做的只是在采取行动之前检查我的回调中是否不是undefined,现在一切正常。
  • 感谢您为更大的背景分享您的想法和解决方案
【解决方案3】:

对于用户按下“回车”时引发问题的一般问题,上述两个回答都是很好的答案。但是 - 我在使用 Google Places Autocomplete 时遇到了一个更具体的问题,这可能是 OP 问题的一部分。为了让 place_changed 事件做任何有用的事情,用户需要选择一个自动完成选项。如果您只是触发 'place_changed',则会跳过 if () 块并且不设置 cookie。

这里对问题的第二部分有一个很好的答案: https://stackoverflow.com/a/11703018/1314762

注意:如果您在同一页面上有多个自动完成输入,则会使用 amirnissim 的答案,而不是选择的答案。

【讨论】:

    【解决方案4】:

    也许不是最用户友好的解决方案,但您可以使用 JQuery 禁用输入键。

    这样的……

    $('.search-location').keypress(function(e) {
      if (e.which == 13) {
        return false;
      }
    });
    

    【讨论】:

    • 我尝试了代码,但认为自动完成在按下键而不是文本框时具有焦点。
    • 不回答问题,并建议做一些非常糟糕的用户体验。
    猜你喜欢
    • 1970-01-01
    • 2014-05-15
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-21
    • 2017-07-10
    • 1970-01-01
    相关资源
    最近更新 更多