【问题标题】:How to format Google Places Autocomplete text pushed into textbox如何格式化推送到文本框中的 Google 地方信息自动完成文本
【发布时间】:2014-03-14 13:43:38
【问题描述】:

我正在使用 Google 将 AutoComplete 放置在文本框上,它基本上可以工作,选择位置并将它们填充到文本框中。

问题是我只想填充选择名称 - 而不是自动完成列表生成的列表中的全名 + 地址格式。我似乎无法找到一种方法来覆盖文本框中的内容。

    var $name = $("#txtName");
    $name.focus();

    var autocomplete = new google.maps.places.Autocomplete($name[0]);
    google.maps.event.addListener(autocomplete, 'place_changed', function () {
        var place = autocomplete.getPlace();

        // explicitly update model
        // but doesn't display in UI
        $scope.locationData.Name = place.name;


        // explicitly update the input box - doesn't update UI
        $name.val(place.name);

        return false;  // doesn't work - updates UI
    });

基本上我想做的是自己接管输入框文本分配,自己完成分配并强制自动完成不设置文本框值。

这可能吗?

【问题讨论】:

    标签: google-maps-api-3 autocomplete google-places-api


    【解决方案1】:

    以短暂的延迟设置值。

    $('#NameBox').on('blur change', function () { $(this).val(place.name).off('blur change'); });
    setTimeout(function () { $('#NameBox').val(place.name); }, 150);
    

    使用.off() 允许用户根据需要编辑名称。如果您希望只允许场所 API 名称删除 .off()

    【讨论】:

      猜你喜欢
      • 2012-10-25
      • 2023-03-26
      • 2019-08-23
      • 2010-09-07
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 2015-07-17
      相关资源
      最近更新 更多