用户输入邮政编码时自动填充状态

时间:2020-09-28 05:42:55

标签: html jquery

我们正在尝试使用USPS API通过传递邮政编码来获取城市和州属性。 API工作正常,但我们需要在会员注册表格中使用此API,用户在其中输入邮政编码,然后应在相应字段中自动填写城市和州。这样,用户不必手动键入城市和州。

我们想知道在Django或其他类型的Web表单中最简单,最快捷的方法是什么。

$(document).ready(function() {
$("#InputZip").keyup(function() {
var el = $(this);
if (el.val().length === 5) {
$.ajax({
url: "https://secure.shippingapis.com/ShippingAPI.dll?API=CityStateLookup&XML= 
<CityStateLookupRequest USERID=""><ZipCode ID='0'><Zip5>80023</Zip5> 
</ZipCode></CityStateLookupRequest>",
cache: false,
dataType: "xml",
type: "GET",
data: "zip=" + el.val(),
success: function(result, success){
$("#Inputstate").val(result.State);
}`enter code here`
});
}});
});

*html*
<input id="InputZip" name="Zip" placeholder="Zip Code" type="text">
<input id="Inputstate" name="state" placeholder="State" type="text">

0 个答案:

没有答案
相关问题