2013-08-16 35 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中有不同的上下文。

相关问题