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

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

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

User Input for Locations

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

不过,我还有一个名为 Edit 的按钮,它会调用 onclick="window.history.back()"

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

这是 Google 自动完成的 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());
    });
  }
}

最佳答案

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

这是一个 fiddle ,显示了执行此操作的简单方法:http://jsfiddle.net/wnbcsrsL/

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

关于javascript - 调用 window.history.back() 时如何维护用户输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30447957/

相关文章:

javascript - Google map 商店定位器 API - TypeError : this. b 未定义

javascript - 将类添加到数组中的项目并删除其他项目

javascript - 谷歌地图不显示

html - Bootstrap - 无法居中 div

javascript - 使用浏览器滚动条代替 iframe 滚动条

node.js - 如何使用 Node.js/Express/MongoDB 向 Google map 添加标记?

javascript - 如何使用 angularjs 在 Kendo 网格过滤器文本框中应用电话号码掩码?

javascript - 使用 MDL 动态构建的输入无法正确呈现

javascript - 在 "Actions must be plain objects. Use custom middleware for async actions."中使用 Jest 模拟结果

javascript - AngularJS - 用圆圈将步行距离添加到 ngMaps