首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在调用window.history.back()时如何维护用户输入?

在调用window.history.back()时如何维护用户输入?
EN

Stack Overflow用户
提问于 2015-05-26 09:09:22
回答 5查看 3.3K关注 0票数 19

我正在处理的一个站点上有两个HTML页面。第一个页面接受用户输入(开始和结束位置),然后将信息传递到Google Maps Javascript API,以确定两个位置之间的距离。

第二个页面为用户显示此信息。

但是,我还有一个调用onclick="window.history.back()"的名为Edit的按钮。

我遇到的问题是,用户输入的两个部分也使用谷歌自动完成地址,所以当我转到下一页并单击Edit按钮时,用户输入将从输入框中删除,而没有谷歌自动完成,它仍然保留在该位置。我假设问题出在Google Autocomplete本身,但是我该如何解决这个问题呢?

这是Google Autocomplete的Javascript:

代码语言:javascript
复制
google.maps.event.addDomListener(window, 'load', initialize);
// ==========================================================================================================
// ==========================================================================================================
// ==========================================================================================================
// USES THE GOOGLE PLACES LIBRARY
// ==============================
// This example displays an address form, using the autocomplete feature
// of the Google Places API to help users fill in the information.

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

function initialize() {
  // Create the autocomplete object, restricting the search
  // to geographical location types.
  autoCompleteOrigin = new google.maps.places.Autocomplete(
      /** @type {HTMLInputElement} */(document.getElementById('start')),
        { types: ['geocode'] });
  autoCompleteDest = new google.maps.places.Autocomplete(
      /** @type {HTMLInputElement} */(document.getElementById('destination')),
      { types: ['geocode'] });
  // When the user selects an address from the dropdown,
  // populate the address fields in the form.
  google.maps.event.addListener(autoCompleteOrigin, 'place_changed', function() {
    fillInAddress();
  });
  google.maps.event.addListener(autoCompleteDest, 'place_changed', function() {
    fillInAddress();
  });
}

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

  for (var component in componentForm) {
    document.getElementById(component).value = '';
    document.getElementById(component).disabled = false;
  }

  // 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 = new google.maps.LatLng(
          position.coords.latitude, position.coords.longitude);
      var circle = new google.maps.Circle({
        center: geolocation,
        radius: position.coords.accuracy
      });
      autocomplete.setBounds(circle.getBounds());
    });
  }
}
EN

回答 5

Stack Overflow用户

发布于 2015-11-04 08:44:43

您必须有一个系统来存储/更新/恢复表单数据。一种方法是让通用的changeinput处理程序将状态保存到localStorage中,然后在页面加载时恢复它(或您可能拥有的任何其他处理程序)

这里有一个小提琴,展示了一种简单的方法:http://jsfiddle.net/wnbcsrsL/

您需要根据您所在的页面进行一些命名空间,以避免名称冲突(还需要确保每个输入都有一个名称),但除此之外,它很简单

票数 1
EN

Stack Overflow用户

发布于 2016-10-12 21:44:34

我通过在提交前删除autocomplete="off"属性解决了这个问题。如果在初始化后已经将其删除,浏览器中的内置自动完成功能(在Chrome上测试)会覆盖google places自动完成列表。这将保留Google的预期行为以及浏览器导航上的输入值。

jQuery解决方案:

代码语言:javascript
复制
$('input[type="submit"]').click(function () {   
    $('input[autocomplete="off"]').removeAttr('autocomplete');
}
票数 1
EN

Stack Overflow用户

发布于 2015-08-30 01:01:24

你能修改给定的Google Autocomplete JS吗?或者这是受限制的供应商代码?

如果是这样的话,我会急于尝试删除这一行:

代码语言:javascript
复制
document.getElementById(component).value = '';

我不认为这与你对历史的使用有任何关系。您可以通过检查编辑按钮的行为是否与浏览器的后退按钮不同来确认这一点。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30447957

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档