![](/img/trans.png)
[英]element.getBoundingClientRect constantly returning the initial value
[英]jQuery is returning the initial value of an element's attribute
情況說明
我的HTML表單加載了數量可變的元素,這些元素的屬性為valid = "default"
。
當用戶為每個元素輸入一個值時,我使用AJAX來驗證標簽並將相應的valid
設置為true或false。
當按下提交按鈕時,我將收集valid
屬性值。
問題
jQuery只返回valid
的初始值。 有沒有辦法獲取valid
的更新值?
我正在使用的代碼:
var tags = ($('.tag').map(function () { return $(this).attr('valid') })).get();
即使值已更新,並且仍應顯示["true", "false"]
返回["default", "default"]
["true", "false"]
我是JavaScript,jQuery和AJAX的新手。 因此,如果有更好的方法可以進行此操作,請告訴我您的想法。
$('#' + field + 'cell')
選擇td
元素, input
的父元素具有valid
屬性,而不是input
元素本身,從而導致
.attr("valid", "false")
應用於父td
元素, 而不應用於子 input
元素。
嘗試刪除DOM
事件屬性,將選擇器更改為
$('#' + field + 'cell')
.attr("style", 'border:3px solid #FF0000')
.find("[id="+field+"]").attr("valid", "false")
$(function() {
function validate(field, query) {
$.ajax({
type: "POST",
url: "/echo/json/",
data: {json:JSON.stringify(query)}
})
.done(function(response) {
console.log(field, response === "false", response === "true")
//response = response.replace(/[[\]]/g,'');
if(response === 'false') {
$('#' + field + 'cell')
.attr("style", 'border:3px solid #FF0000')
.find("[id="+field+"]").attr("valid", "false")
}
else {
$('#' + field + 'cell')
.attr("style", 'border: none')
.find("[id=" + field + "]")
.attr("valid", "true")
}
})
.fail(function() {
$('#response').html('error');
})
.always(checkForm) // added to verify `checkForm` at each `$.ajax` call
}
function checkForm(variable) {
var tags = $.map($(".tag"), function (el) { return $(el).attr('valid') });
console.log(tags);
}
$("#submitButton").on("click", function(e) {
checkForm()
});
$("[id^=tag]").on("blur", function(e) {
validate(e.target.id, e.target.value)
});
});
jsfiddle http://jsfiddle.net/mL8ttwc0/2/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.