javascript 根据邮政编码自动填充州和城市

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22564175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:22:15  来源:igfitidea点击:

Autofilling state and city based on zip code

javascriptjqueryajaxjsonforms

提问by user3361043

I am new to web development and I ran into an issue with a registration form that I'm building for our business. I am using this guide: http://css-tricks.com/using-ziptastic/. This is my first experience with AJAX and JSON and I just can't get the city and state to autofill as soon as I'm done typing in the zip code. Any solutions/suggestions/alternatives will be much appreciated. I set up a basic jsfiddle to test everything out: http://jsfiddle.net/7VtHc/

我是 Web 开发的新手,我在为我们的业务构建的注册表中遇到了问题。我正在使用本指南:http: //css-tricks.com/using-ziptastic/。这是我第一次使用 AJAX 和 JSON,我无法在输入邮政编码后立即自动填充城市和州。任何解决方案/建议/替代方案将不胜感激。我设置了一个基本的 jsfiddle 来测试一切:http: //jsfiddle.net/7VtHc/

HTML:

HTML:

<p>Zip Code: <input type="text" id="zip" /></p>
<p>City: <input type="text" id="city" /></p>
<p>State: <input type="text" id="state" /></p>

jQuery, etc.

jQuery 等

$("#zip").keyup(function() {
var el = $(this);

if ((el.val().length == 5) && (is_int(el.val()))) {
    $.ajax({
    url: "http://zip.elevenbasetwo.com",
    cache: false,
    dataType: "json",
    type: "GET",
    data: "zip=" + el.val(),
    success: function(result, success)

    $("#city").val(result.city);
    $("#state").val(result.state);
    });
}
});

回答by Ashley Medway

This should work for you. http://jsfiddle.net/7VtHc/5/
I will added that ===in JavaScript will check that type are the same. See Here.
There is no need to create an is_int()function.

这应该对你有用。http://jsfiddle.net/7VtHc/5/
我会补充说,===在 JavaScript 中会检查类型是否相同。请参阅此处
无需创建is_int()函数。

$(document).ready(function() {
    $("#zip").keyup(function() {
        var el = $(this);

        if (el.val().length === 5) {
            $.ajax({
                url: "http://zip.elevenbasetwo.com",
                cache: false,
                dataType: "json",
                type: "GET",
                data: "zip=" + el.val(),
                success: function(result, success) {
                    $("#city").val(result.city);
                    $("#state").val(result.state);
                }
            });
        }
    });
});

P.S. When you create a JSFiddle make sure you include jQuery from the menu on the right.

PS 创建 JSFiddle 时,请确保从右侧菜单中包含 jQuery。

回答by tamilselvan s

For India you can use pincode API for free, You can get the city with other information as JSON format.

对于印度,您可以免费使用 pincode API,您可以以 JSON 格式获取带有其他信息的城市。

Example: https://api.postalpincode.in/pincode/641001

示例:https: //api.postalpincode.in/pincode/641001

Usage: https://api.postalpincode.in/pincode/${Your-pincode}

用法: https://api.postalpincode.in/pincode/${Your-pincode}

If any cors Policy occured means use this :

如果发生任何 cors Policy 意味着使用这个:

https://cors-anywhere.herokuapp.com/https://api.postalpincode.in/pincode/${Your-Pincode}

https://cors-anywhere.herokuapp.com/https://api.postalpincode.in/pincode/${Your-Pincode}

Example:

例子:

{
  "Name": "Coimbatore",
  "Description": null,
  "BranchType": "Head Post Office",
  "DeliveryStatus": "Delivery",
  "Circle": "Tamilnadu",
  "District": "Coimbatore",
  "Division": "Coimbatore",
  "Region": "Coimbatore",
  "Block": "Coimbatore South",
  "State": "Tamil Nadu",
  "Country": "India",
  "Pincode": "641001"
}

I am using this in Angular-8Thanks.

我在Angular-8感谢中使用它。