2013-08-16 69 views
2

我有一個實現谷歌的地方自動完成和填充其他文本字段的形式。谷歌的地方自動完成和模糊的問題

<script type="text/javascript"> 
    $(document).ready(function(){ 
     $('.noEnterSubmit').keypress(function(e){ 
      if (e.which == 13) return false; 
     }); 

     var updateAddress = { 

     autocomplete: new google.maps.places.Autocomplete($("#Subscription_address")[0], {types: ['geocode']}), 

     event: function(){ 
      var self = this;  
      google.maps.event.addListener(self.autocomplete, 'place_changed', function() { 
       var place = self.autocomplete.getPlace(), 
        address = place.address_components, 
        streetAddress = '', 
        suburb = '', 
        state = '', 
        zip = '', 
        country = ''; 

       for (var i = 0; i < address.length; i++) { 
        var addressType = address[i].types[0]; 

        if (addressType == 'subpremise') { 
         streetAddress += address[i].long_name + '/'; 
        } 
        if (addressType == 'street_number') { 
         streetAddress += address[i].long_name + ' '; 
        } 
        if (address[i].types[0] == 'route') { 
         streetAddress += address[i].long_name; 
        } 
        if (addressType == 'locality') { 
         suburb = address[i].long_name; 
        } 
        if (addressType == 'administrative_area_level_1') { 
         state = address[i].long_name; 
        } 
        if (addressType == 'postal_code') { 
         zip = address[i].long_name; 
        } 
        if (addressType == 'country') { 
         country = address[i].long_name; 
        } 
       } 

       // update the textboxes 
       setTimeout(function(){$('#Subscription_address').val(streetAddress).blur(function(e){ 
        this.value = this.streetAddress; 
       });},50); 
       $('#Subscription_city').val(suburb); 
       $('#Subscription_state').val(state); 
       $('#Subscription_zip').val(zip); 
       $('#Subscription_country').val(country); 
      }); 

     } 
    }; 

    updateAddress.event(); 

    }); 
    </script> 
    </head> 

<input class="noEnterSubmit input-large" name="Subscription[address]" id="Subscription_address" type="text"> 

我阻止表單提交輸入按鍵。只有當用戶輸入按鍵則焦點從文本字段並在#Subscription_address重置和谷歌Places API的顯示#Subscription_address「未定義」值。

目前標籤按鍵和鼠標選擇如預期來填充等領域,並在#Subscription_address場只保留了一個的StreetAddress模糊事件的工作。

使用jQuery如何允許用戶使用輸入按鍵則焦點從文本字段,並保留選擇的值來選擇自動完成的結果?

回答

0
 // update the textboxes 
     setTimeout(function(){$('#Subscription_address').val(streetAddress).blur(function(e){ 
      this.value = streetAddress; 
     });},50); 

我需要在模糊功能後刪除this.streetAddress。這在下拉菜單的DOM中有不同的上下文。

相關問題