gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:47:54 24 4
gpt4 key购买 nike

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

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 等

$("#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);
});
}
});

最佳答案

这应该适合您。 http://jsfiddle.net/7VtHc/5/
我将添加 JavaScript 中的 === 将检查类型是否相同。 See Here .
无需创建 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);
}
});
}
});
});

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

关于javascript - 根据邮政编码自动填充州和城市,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22564175/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com