如何为 Google 在 Enter 键上自动完成地方触发 place_changed 事件 [英] How to fire place_changed event for Google places auto-complete on Enter key

查看:16
本文介绍了如何为 Google 在 Enter 键上自动完成地方触发 place_changed 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The click seems to fire the event and set the cookies but pressing enter to submit doesn't set the cookies and instead the page redirects without the cookies.

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 中获取值并重定向(服务器端)

The .search-location is a class on multiple textboxes. There is a submit button that takes the values from the cookies and redirects (server side)

推荐答案

改编自 Jonathan Caulfield 的回答:

Adapted from Jonathan Caulfield's answer:

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

这篇关于如何为 Google 在 Enter 键上自动完成地方触发 place_changed 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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