2016-06-08 25 views
0

我無法使用jquery .val()獲取自動完成輸入,因爲.val()只返回由我自己鍵入的輸入值,而不是自動完成添加的內容。如何使用jquery .val()從自動完成輸入中獲取值?

jquery .val()只在其他字段進行更改時才返回完整值。

我使用谷歌地圖自動完成地址,並比我通過該地址谷歌地圖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> 

回答

0

由我自己解決。只是使用jquery觸發器方法模擬表單更改。

$("#anyInput").trigger("change"); 
相關問題