[英]update variable in jQuery after AJAX call
在有人resubmits the AJAX call without a page refresh
之前,我的AJAX功能resubmits the AJAX call without a page refresh
正常工作。 在這種情況下,AJAX調用使用var amount的舊值而不是更新后的值。 因此,我需要在成功函數的末尾更新var數量,但未能這樣做。
$(document).on('click', '#updateBidButton', function (e) {
e.preventDefault();
var amount = ('#curr_bid').val()
var expire_date = "<?php echo $this->item['expire_date']?>";
$.ajax({
type: 'post',
url: "?module=items&controller=index&action=submit",
dataType: "text",
data: 'amount=' + amount + '&expire_date=' + expire_date,
beforeSend: function () {
$('.auction_box').animate({
'backgroundColor': '#ffdead'
}, 400);
},
success: function (result) {
if (result == 'ok') {
$('.auction_box').animate({
'backgroundColor': '#A3D1A3'
}, 500);
amount = $('#curr_bid').val();
setTimeout(function () {
$('.auction_box').css('background-color', '#FFF');
} , 5000);
}
}
});
});
在您的代碼量變量未更新中,使用
var newValue = ''; // for example get new value from ajax response
$('#curr_bid').val(newValue)
用於更新
在這里你有一個解決方案
$(document).on('click', '#updateBidButton', function (e) {
e.preventDefault();
var amount = $('#curr_bid').val()
var expire_date = "<?php echo $this->item['expire_date']?>";
$.ajax({
type: 'post',
url: "?module=items&controller=index&action=submit",
dataType: "text",
data: 'amount=' + amount + '&expire_date=' + expire_date,
beforeSend: function () {
$('.auction_box').animate({
'backgroundColor': '#ffdead'
}, 400);
},
success: function (result) {
if (result == 'ok') {
$('.auction_box').animate({
'backgroundColor': '#A3D1A3'
}, 500);
setTimeout(function () {
$('.auction_box').css('background-color', '#FFF');
} , 5000);
}
}
});
});
第三行缺少$
。 在成功方法中,無需將值重新分配給amount
變量。
單擊時, amount variable
將從input#curr_bid
獲取最新值。
希望這會幫助你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.