简体   繁体   中英

Populate zip code field from url variable using jquery/javascript

I am trying to figure out how to populate zip code field on form from url variable using jquery/javascript and struggling on how to achieve this. I have a jsfiddle of my code: https://jsfiddle.net/jsavage/Lumxgm92/9/ . I did a similar thing for populating the email address on a form from the url parameter but I can't figure out how to do the same for populating zip code on a form from url parameter. Any help would be greatly appreciated! Thank you!

 <!--- 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" />

You need to reference this:

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

Do this:

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

This all returns a string containing the city and state, but you can adjust this to your needs.

What is that regex supposed to be doing? It looks like it just replaces all the alphanumerics in your retrieved value with "".

EDIT:

Scratch that. It replaces escaped html chars like %20.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM