使用jquery/javascript从url变量填充邮政编码字段

使用jquery/javascript从url变量填充邮政编码字段,javascript,jquery,Javascript,Jquery,我正试图弄清楚如何使用jquery/javascript从url变量填充表单上的邮政编码字段,并努力研究如何实现这一点。我有一段代码:。对于从url参数填充表单上的电子邮件地址,我做了类似的事情,但对于从url参数填充表单上的邮政编码,我不知道如何做同样的事情。任何帮助都将不胜感激!谢谢大家! <!--- BEGIN POPULATE ZIPCODE FIELD FROM URL VARIABLE CODE ---> Y.use('jquery-noconflict', fu

我正试图弄清楚如何使用jquery/javascript从url变量填充表单上的邮政编码字段,并努力研究如何实现这一点。我有一段代码:。对于从url参数填充表单上的电子邮件地址,我做了类似的事情,但对于从url参数填充表单上的邮政编码,我不知道如何做同样的事情。任何帮助都将不胜感激!谢谢大家!

 <!--- BEGIN POPULATE ZIPCODE FIELD FROM URL VARIABLE CODE --->

  Y.use('jquery-noconflict', function() {

    function getQuerystring(key, default_) {
        if (default_ == null) default_ = "";
        key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
        var qs = regex.exec(decodeURIComponent (window.location.href));
        if (qs == null)
            return default_;
        else
            return qs[1];
    }

    var other_value = getQuerystring('zipcode');
    other_value = other_value.replace(/(%[a-zA-Z0-9].)/g, "");
    other_value = other_value.replace(/(\+)/g, "");
    jQuery('#billing_addr_zipname').val(other_value);
});  

Y.use('jquery-noconflict',function(){
函数getQuerystring(键,默认值){
如果(默认值=null)默认值;
key=key.replace(/[\[]/,“\\\[”)。replace(/[\]]/,“\\\]”);
var regex=new RegExp(“[\\?&]”+key+“=([^&\]*)”;
var qs=regex.exec(decodeURIComponent(window.location.href));
如果(qs==null)
返回默认值;
其他的
返回qs[1];
}
var other_value=getQuerystring('zipcode');
其他值=其他值。替换(/([a-zA-Z0-9])/g,“”;
其他值=其他值。替换(/(\+)/g,“”);
jQuery(“#账单地址_zipname”).val(其他值);
});  

ZIPCODE:

您需要参考以下内容:

<script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=true"></script>

这样做:

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;
    }
var-zip=;
var lat;
乏液化天然气;
var geocoder=new google.maps.geocoder();
geocoder.geocode({'address':zip},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
geocoder.geocode({'latLng':结果[0].geometry.location},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
如果(结果[1]){
var loc=getCityState(结果);
}
}
});
}
}); 
函数getCityState(结果)
{
var a=结果[0]。地址\u组件;
var市、州;
对于(i=0;i

所有这些都返回一个包含城市和州的字符串,但您可以根据需要进行调整。

正则表达式应该做什么?看起来它只是将检索到的值中的所有字母数字替换为“”

编辑:


把它擦掉。它将替换转义的html字符,如%20。

请指定该过程中失败的步骤。您是否从
getQuerystring('zipcode')
中获得了一个值?首先应该是一个注释
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;
    }