0

因此,我有一個谷歌地圖自動填充的形式,當它得到一個地方,它需要的地方和格式,以便所有正確的信息進入不同的輸入表格。如何停止從谷歌地點填充自動填充的地址

我遇到的問題是在'place_changed'事件激發自動填充填充輸入之前。我想阻止最初的人口,而是用我想在那裏的東西來填充。

我試着做一個改變事件,但它似乎沒有正確開火。這裏是我的代碼:

HTML:

<input type="text" name="address1" value="1255 W Washington St" required="" class="form-control" id="id_address1" placeholder="Enter a location" autocomplete="var input = /** @type {HTMLInputElement} */(

JS:

document.getElementById('id_address1')); 
var autocomplete = new google.maps.places.Autocomplete(input); 
google.maps.event.addDomListener(input, 'keydown', function(e) { 
     if (e.keyCode == 13) { 
      e.preventDefault(); 
      console.log('enter'); 
     } 
}); 
google.maps.event.addDomListener(input, 'focusout', function(e) { 
    if ($('#id_address1').val() != $('#id_address1').attr('value')){ 
     $('#id_address1').val($('#id_address1').attr('value')); 
    } 
});off" 

google.maps.event.addListener(autocomplete, 'place_changed', function() { 
    var place = autocomplete.getPlace(); 
    if (place.address_components) { 
     var address = ''; 

     for (var i = 0; i < place.address_components.length; i++){ 
      switch (place.address_components[i].types[0]){ 
       case 'street_number': 
        address = place.address_components[i].short_name; 
        break; 
       case 'route': 
        address += ' ' + place.address_components[i].short_name; 
        break; 
       case "locality": 
        $('#id_city').val(place.address_components[i].short_name); 
        break; 
       case "administrative_area_level_1": 
        $('#id_state').val(place.address_components[i].short_name); 
        break; 
       case "country": 
        $('#id_country').val(place.address_components[i].short_name); 
        break; 
       case "postal_code": 
        $('#id_zip_code').val(place.address_components[i].long_name); 
        break; 
       } 
     } 
     $('#id_address1').val(address); 
     $('#id_address1').attr('value', address); 
} 

現在當設置ATTR,然後對事件的內容檢查是一種解決方法,因爲當我選擇只使用一個地方它嘗試輸入原始完整字符串的鍵盤。

回答

4

在設置值之前觸發模糊事件:

$('#id_address1').trigger('blur').val(address); 
+0

OMG。一百萬瓶啤酒給你!奇蹟般有效。 – Foxinni 2015-03-25 14:52:12