【发布时间】:2011-05-16 18:41:01
【问题描述】:
您好,当用户输入 5/9 位邮政编码时,我有一个邮政编码字段,它应该自动填充州和城市字段。
在 javascript 或 JQuery 中是否有类似的东西???
谢谢
【问题讨论】:
标签: javascript jquery google-maps jquery-plugins
您好,当用户输入 5/9 位邮政编码时,我有一个邮政编码字段,它应该自动填充州和城市字段。
在 javascript 或 JQuery 中是否有类似的东西???
谢谢
【问题讨论】:
标签: javascript jquery google-maps jquery-plugins
使用 Javascript Google Maps V3 API:
你需要参考这个:
<script type="text/javascript" src="http://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;
}
这一切都以这种格式返回一个包含城市和州的字符串,但您可以根据需要进行调整。
【讨论】:
https://,这样您的https:// 站点在发出http:// 请求时不会收到黄色警告标签。邮政编码是敏感信息,因此不希望用户收到错误标记。
在 Css-tricks.com 上找到一篇文章,功能是使用 Ziptastic 实现的 http://css-tricks.com/using-ziptastic/
$.ajax({
url: "http://zip.elevenbasetwo.com",
cache: false,
dataType: "json",
type: "GET",
data: "zip=" + el.val(),
success: function(result, success) {
$(".fancy-form div > div").slideDown(); /* Show the fields */
$("#city").val(result.city); /* Fill the data */
$("#state").val(result.state);
$(".zip-error").hide(); /* In case they failed once before */
$("#address-line-1").focus(); /* Put cursor where they need it */
},
error: function(result, success) {
$(".zip-error").show(); /* Ruh row */
}
});
【讨论】:
RedLine 有一个邮政编码 API,可以免费执行此操作。他们在这里有示例代码http://zipcodedistanceapi.redline13.com/Examples。
【讨论】:
感谢所有答案,如果您不想陷入更复杂的代码,这里有一个适合您的解决方案。
第一步:
下载最新的国家/地区邮政编码Excel文件并在根文件夹下找到该excel文件。(您可以在这里下载)
https://data.gov.in/catalog/all-india-pincode-pirectory#web_catalog_tabs_block_10)
第二步:
调用 ajax 函数你的表单在哪里
<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type=”text/javascript”><br>
jQuery(document).ready(function(){
jQuery(‘.postcode‘).blur(function(){ //.postcode class of zipcode text field
var s = jQuery(this).val();
jQuery.ajax({
type: ‘POST’,
url:“http://www.sample.com/postcode.php“, //file which read zip code excel file
dataType: “json”, //is used for return multiple values
data: { ‘s’ : s },
success: function(data){
try {
jQuery(“.state“).val(data.state); //region-class of state text field
jQuery(“.city“).val(data.dist);//city-class of city text filed
} catch (e) {
alert(e.Message);
}
},
error:function (xhr, status, err){
alert( “status=” + xhr.responseText + “, error=” + err );
}
});
});
});
</script>
此 Ajax 函数将调用“http://www.sample.com/postcode.php”文件
在 postcode.php 文件下,我们必须读取 excel 文件并返回州和城市值
postcode.php
<?php
extract ($_POST);
$s=$_POST[‘s’]; //get value from ajax
$filename=”zip.csv“; //zipcode csv file(must reside in same folder)
$f = fopen($filename, “r”);
while ($row = fgetcsv($f))
{
if ($row[1] == $s) //1 mean number of column of zipcode
{
$district=$row[3]; //3- Number of city column
$state=$row[4]; //4-Number of state column
break;
}
}
fclose($f);
echo json_encode(
array(“dist” => $district,
“state” => $state,
“zip” => $s)
); //Pass those details by json
?>
就是这样,享受吧
【讨论】: