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

Populate zip code field from url variable using jquery/javascript

本文关键字:变量 填充 邮政编码 字段 url jquery javascript 使用      更新时间:2023-09-26

我正试图弄清楚如何使用jquery/javascript从url变量中填充邮政编码字段,并努力实现这一目标。我有我的代码:https://jsfiddle.net/jsavage/Lumxgm92/9/。我做了类似的事情,从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);
});  

    <label for="billing_addr_zipname">ZIPCODE:</label>
    <input type="text" name="billing_addr_zipname" id="billing_addr_zipname" value="" maxlength="50" />

你需要引用这个:

<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;
    }

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

这个正则表达式应该在做什么?看起来它只是用"替换了检索值中的所有字母数字。

编辑:

划痕。它取代转义的html字符,如%20。