Я не могу получить ввод автозаполнения с jquery .val(), потому что .val() возвращает только значение ввода, которое было введено мной, но не то, что было добавлено автозаполнением.Как получить значение из ввода автозаполнения с помощью jquery .val()?
jquery .val() возвращает только полное значение, если внести изменения в другие поля.
Я использую google maps autocomplete для адреса, а затем передаю этот адрес в google maps api, чтобы получить расстояние между двумя адресами. Проблема заключается в том, что этот «start = $ ('# id_rent_delivery_address»). Val() «получает только адресную часть, которая вводится вручную, но не автозаполняется.
Возможно, у вас есть идеи, как я могу это исправить?
Мой код:
<script>
$(document).ready(function() {
initAutocomplete();
});
var autocomplete, autocomplete2;
function initAutocomplete() {
// Create the autocomplete object, restricting the search to geographical
// location types.
autocomplete = new google.maps.places.Autocomplete(
/** @type {!HTMLInputElement} */(document.getElementById('id_rent_withdraw_address')),
{types: ['geocode']});
autocomplete2 = new google.maps.places.Autocomplete(
/** @type {!HTMLInputElement} */(document.getElementById('id_rent_delivery_address')),
{types: ['geocode']});
// When the user selects an address from the dropdown, populate the address
// fields in the form.
}
</script>
<script type="text/javascript">
$(document).on('change', '#rent-confirm-form', function() {
var start = $('#id_rent_delivery_address').val();
var end = $('#id_rent_withdraw_address').val();
GetRoute(start, end);
});
function GetRoute(start, end) {
//*********DISTANCE AND DURATION**********************//
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix({
origins: [start],
destinations: [end],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false
}, function (response, status) {
if (status == google.maps.DistanceMatrixStatus.OK && response.rows[0].elements[0].status != "ZERO_RESULTS") {
var distance = response.rows[0].elements[0].distance.text;
var duration = response.rows[0].elements[0].duration.text;
console.log(distance);
console.log(duration);
console.log(start);
console.log(end);
} else {
console.log("Unable to find the distance via road.");
}
});
}
</script>