jquery - jQuery .on("keyup") only fires once after ajax call, does not update if value is changed, using it up update city & state with zipcode
问题描述
This is my jquery with ajax call
$(".zipfield").on("keyup", function(){
var city = $(this).closest(".addressBlock").find(".cityfield");
var state = $(this).closest(".addressBlock").find(".statefield");
if ($(this).val() !== "" && (city.val() === "" || city.val() == null) && (state.val() === "" || state.val() == null) ) {
$.ajax({
method: "POST",
url: "<?= $this->baseUrl();?>/search/zip",
data: {zip: $(this).val()},
complete: function (r, e) {
var data = JSON.parse(r.responseText);
if (data.status === "OK") {
city.val(data.city);
state.val(data.state);
}
}
});
}
});
any suggestions as to how I can get it updated everytime I input a new value for the zipfield
解决方案
似乎在行中
var city = $(this).closest(".addressBlock").find(".cityfield");
var state = $(this).closest(".addressBlock").find(".statefield");
using$(this).closest(".className")
实际上并没有获取元素,因此在您的 ajax 调用之后没有设置它们。
我不相信你可以closest
在 jquery 中使用类名。您可能应该找到最接近的tr
,或者甚至可以form
代替。从那里你可以使用find
类名cityfield
和statefield
.
推荐阅读
- angular - 离子 - API 状态 404
- android - React Native CLI 无法确定 Android 项目配置
- java - 基于指定类类型返回值的 Java 通用双函数
- javascript - !()在javascript中是什么意思
- adal - 使用 msal 对“management.azure.com”进行身份验证
- jquery - 需要双击才能在 Bootstrap 表单上进行编辑。jQuery?
- sql - 子查询,不必重用代码
- r - 在列表上应用函数然后迭代第二个变量,在 r
- excel - 尝试更新 excel 链接但看到一个没有意义的弹出框
- select - MySQL 5.6 SELECT UNION 查询