使用jquery/javascript从url变量填充邮政编码字段
Populate zip code field from url variable using jquery/javascript
我正试图弄清楚如何使用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。
相关文章:
- 如何使用变量填充iframe源
- JavaScript:如何用变量填充单选按钮值和标签
- 上下文的变量.填充文本
- 自定义 css 属性或部分 css 变量填充程序
- 从 URL 变量填充电子邮件字段
- 将Jade模板变量填充到内联函数中
- node.js中的变量填充和回调
- 用javascript变量填充jquery模态表单字段值
- onClick由变量填充而不触发它
- 我可以打开一个新窗口并用字符串变量填充它吗
- 从另一个变量填充值的 JavaScript
- 根据URL字符串中的变量填充属性值
- 在返回之前用变量填充javascript数组
- 用动态变量填充ng-repeat中的数据
- 用JavaScript变量填充HTML标签
- 从url变量填充输入字段-使用javascript或jquery
- 用枚举变量填充一个数组
- 用Javascript变量填充元内容属性
- 从url查询字符串中的变量填充Javascript表单字段
- 使用ajax请求变量填充下拉菜单