【问题标题】:How to clear search input field with javascript when using Google Maps v3 Autocomplete Form使用 Google Maps v3 自动完成表单时如何使用 javascript 清除搜索输入字段
【发布时间】:2018-03-16 00:03:52
【问题描述】:

我正在使用 Google maps v3 api 自动完成地址表单,但我不想使用示例中的一般搜索字段 (https://google-developers.appspot.com/maps/documentation/javascript/examples/full/places-autocomplete-addressform)。

我正在尝试将街道地址字段作为搜索字段以及街道编号和街道名称字段。我在 JS fiddle (https://jsfiddle.net/smartystreets/0c9fy73v/) 上找到了这个例子,我无法复制它。

我尝试关闭 autocomplete=off 但在 chrome 上不起作用(对于 elementId('autocomplete'))

我尝试隐藏路线和街道输入元素并将输入 id 值设置为空。

document.getElementById('autocomplete').value = ''; 

然后使用隐藏的输入字段来重建表单输入 w/

document.getElementById('autocomplete').value = document.getElementById('street_number') + ' ' + document.getElementById('route');

任何想法我做错了什么?为什么搜索栏不清晰?

这是我的搜索/地址字段的 HTML:

<!-- address -->
   <div class="col-md-6 col-lg-6">
      <label class="light text-capitalize">address:</label>
      <input type="text"   id="autocomplete"
         name="street" placeholder="Street address, PO Box, etc.">
      <input type="hidden" id="route" disabled="false">
      <input type="hidden" id="street_number" disabled="false"> 
      <input type="text"  name="address1" placeholder="(Optional) Apartment, Suite, Floor, etc.">
   </div>
<!-- address end -->

这是我的内联 JS

  var placeSearch, autocomplete;
  var componentForm = {
     street_number: 'short_name',
     route: 'long_name',
     locality: 'long_name',
     administrative_area_level_1: 'short_name',
     postal_code: 'short_name'
  };

  function initAutocomplete() {
    // Create the autocomplete object, restricting the search to geographical
    // location types.
    autocomplete = new google.maps.places.Autocomplete(
        /** @type {!HTMLInputElement} */(document.getElementById('autocomplete')),
        {types: ['geocode']});

    // When the user selects an address from the dropdown, populate the address
    // fields in the form.
    autocomplete.addListener('place_changed', fillInAddress);
  }

  function fillInAddress() {
    // Get the place details from the autocomplete object.
    var place = autocomplete.getPlace();


    for (var component in componentForm) {

    document.getElementById('street_number').value = '';
    document.getElementById('route').value = '';
    document.getElementById('locality').value = '';
    document.getElementById('administrative_area_level_1').value = '';
//  document.getElementById('country').value = '';
    document.getElementById('postal_code').value = '';

    }

    // Get each component of the address from the place details
    // and fill the corresponding field on the form.
   for (var i = 0; i < place.address_components.length; i++) {
    var addressType = place.address_components[i].types[0];
      if (componentForm[addressType]) {
       var val = place.address_components[i][componentForm[addressType]];
      document.getElementById(addressType).value = val;
      }
    }
  }

  // Bias the autocomplete object to the user's geographical location,
  // as supplied by the browser's 'navigator.geolocation' object.
  function geolocate() {
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(function(position) {
        var geolocation = {
          lat: position.coords.latitude,
          lng: position.coords.longitude
        };
        var circle = new google.maps.Circle({
          center: geolocation,
          radius: position.coords.accuracy
        });
        autocomplete.setBounds(circle.getBounds());
      });
    }
  }
</script>

【问题讨论】:

  • 什么时候清除搜索栏?
  • 嗨詹姆斯,我想在用户点击与他们的搜索匹配的地址后清除它

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


【解决方案1】:

如果您真的想在用户点击自动完成的地址后清除搜索输入字段,请尝试在获得地点后将输入 (id=autocomplete) 值设置为空。

function fillInAddress() {
    // Get the place details from the autocomplete object.
    var place = autocomplete.getPlace();

    /* Your other code */

    document.getElementById('autocomplete').value = '';
}

我只是为你做了一个演示来检查行为。你可以在这里看到它http://jsbin.com/mugasav/1/edit?html,js,output

【讨论】:

  • 詹姆斯你是个传奇。完全按预期工作,我把头撞在墙上好几个小时试图弄清楚!
  • 如果我的回答对你有帮助,你也可以接受我的回答,这样对我和其他人也有帮助:)
  • 这应该有更多的支持,让我免于自动完成重新初始化的丑陋黑客攻击
猜你喜欢
  • 1970-01-01
  • 2018-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-22
  • 1970-01-01
  • 2017-11-16
  • 2014-10-07
相关资源
最近更新 更多