使用邮政编码自动填写州城市

use*_*663 15 javascript jquery google-maps jquery-plugins

嗨我有一个邮政编码字段,当用户输入一个5/9数字的拉链时,它应该自动填充州和城市字段.

有没有像javascript或JQuery这样的东西???

谢谢

sla*_*dau 18

使用Javascript Google Maps V3 API:

你需要参考这个:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
Run Code Online (Sandbox Code Playgroud)

这样做:=

    var zip = <yourzipcode>;
    var lat;
    var lng;
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({ 'address': zip }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            geocoder.geocode({'latLng': results[0].geometry.location}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                if (results[1]) {
                    var loc = getCityState(results);
                }
            }
        });
        }
    }); 

function getCityState(results)
    {
        var a = results[0].address_components;
        var city, state;
        for(i = 0; i <  a.length; ++i)
        {
           var t = a[i].types;
           if(compIsType(t, 'administrative_area_level_1'))
              state = a[i].long_name; //store the state
           else if(compIsType(t, 'locality'))
              city = a[i].long_name; //store the city
        }
        return (city + ', ' + state)
    }

function compIsType(t, s) { 
       for(z = 0; z < t.length; ++z) 
          if(t[z] == s)
             return true;
       return false;
    }
Run Code Online (Sandbox Code Playgroud)

这将以此格式返回包含城市和州的字符串,但您可以根据需要进行调整.