简体   繁体   English

如何使用jquery .val()从自动完成输入中获取价值?

[英]How to get value from autocomplete input with jquery .val()?

I can't get autocomplete input with jquery .val(), because .val() only returns input value which was typed by myself but not what was added by autocomplete. 我无法使用jquery .val()获得自动完成输入,因为.val()仅返回自己输入的输入值,而不返回自动完成添加的输入值。

jquery .val() only returns full value if make changes on other fields. 如果对其他字段进行更改,则jquery .val()仅返回完整值。

I use google maps autocomplete for address, and than I pass that address to google maps api to get distances between two addresses. 我使用google maps自动完成功能作为地址,然后我将该地址传递给google maps api以获取两个地址之间的距离。 Problem is that this "start = $('#id_rent_delivery_address').val()" only gets address part which is typed manually but not autocompleted. 问题是,此“开始= $('#id_rent_delivery_address')。val()”仅获得手动键入但未自动完成的地址部分。

Maybe you have any ideas how I could fix it? 也许您有任何想法我该如何解决?

My code: 我的代码:

<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>

Solved by myself. 由我自己解决。 Just simulated form change with jquery trigger method. 只是使用jquery触发方法模拟了表单更改。

$( "#anyInput" ).trigger("change");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM