我有这个输入字段
<input type="text" id="location">
<button id="getLocation">Get Location</button>
在我的js中
$(document).ready(function(){
$('#getLocation').on('click', function(){
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
this.location = 'Geolocation is not supported by this device';
}
});
function showPosition(position) {
console.log("Latitude: " + position.coords.latitude);
console.log("Longitude: " + position.coords.longitude);
var latlng = {lat: parseFloat(position.coords.latitude), lng: parseFloat(position.coords.longitude)};
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'location': latlng}, function(results, status) {
if (status === 'OK') {
if (results[1]) {
console.log(results[1].formatted_address);
$('#location').val(results[1].formatted_address);
} else {
window.alert('No results found');
}
} else {
window.alert('Geocoder failed due to: ' + status);
}
});
}
});
当我执行此操作console.log(results[1].formatted_address);
时,它会在控制台中显示地址,但是当我执行此操作$('#location').val(results[1].formatted_address);
时,它不会显示在输入文本字段中。
如何显示此 - &gt;输入文本字段results[1].formatted_address
<input type="text" id="location">
感谢您的帮助:)