我正在处理的一个站点上有两个HTML页面。第一个页面接受用户输入(开始和结束位置),然后将信息传递到Google Maps Javascript API,以确定两个位置之间的距离。
第二个页面为用户显示此信息。
但是,我还有一个调用onclick="window.history.back()"
的名为Edit
的按钮。
我遇到的问题是,用户输入的两个部分也使用谷歌自动完成地址,所以当我转到下一页并单击Edit
按钮时,用户输入将从输入框中删除,而没有谷歌自动完成,它仍然保留在该位置。我假设问题出在Google Autocomplete本身,但是我该如何解决这个问题呢?
这是Google Autocomplete的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());
});
}
}
发布于 2015-11-04 08:44:43
您必须有一个系统来存储/更新/恢复表单数据。一种方法是让通用的change
和input
处理程序将状态保存到localStorage
中,然后在页面加载时恢复它(或您可能拥有的任何其他处理程序)
这里有一个小提琴,展示了一种简单的方法:http://jsfiddle.net/wnbcsrsL/
您需要根据您所在的页面进行一些命名空间,以避免名称冲突(还需要确保每个输入都有一个名称),但除此之外,它很简单
发布于 2016-10-12 21:44:34
我通过在提交前删除autocomplete="off"
属性解决了这个问题。如果在初始化后已经将其删除,浏览器中的内置自动完成功能(在Chrome上测试)会覆盖google places自动完成列表。这将保留Google的预期行为以及浏览器导航上的输入值。
jQuery解决方案:
$('input[type="submit"]').click(function () {
$('input[autocomplete="off"]').removeAttr('autocomplete');
}
发布于 2015-08-30 01:01:24
你能修改给定的Google Autocomplete JS吗?或者这是受限制的供应商代码?
如果是这样的话,我会急于尝试删除这一行:
document.getElementById(component).value = '';
我不认为这与你对历史的使用有任何关系。您可以通过检查编辑按钮的行为是否与浏览器的后退按钮不同来确认这一点。
https://stackoverflow.com/questions/30447957
复制相似问题