My AJAX function is below working successfully until someone resubmits the AJAX call without a page refresh
. In this case, the AJAX call uses the old value of var amount rather than an updated one. Therefore, I need to update var amount at the end of my success function but am failing to do so.
$(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);
}
}
});
});
in your code amount variable not updated, use
var newValue = ''; // for example get new value from ajax response
$('#curr_bid').val(newValue)
for updating
Here you go with a solution
$(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);
}
}
});
});
Missing $
in 3rd line. No need to reassign the value to amount
variable in success method.
As you click amount variable
will get the latest value from input#curr_bid
.
Hope this will help you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.