jquery - ajax成功后如何“添加到购物车”按钮更改文本“已添加”
问题描述
我是新的 PHP 开发人员,我致力于购物车功能“添加到购物车”按钮单击数据成功保存在数据库表中。但是“添加到购物车”按钮文本没有改变。如何解决问题?
jQuery:
var addedCart = 0;
function advAddtocart(a_id){
if (addedCart==1) {
$("#paymentCart_"+a_id).html('Added');
}
var $this = $("#paymentCart_"+a_id);
$($this).buttonLoader('start');
setTimeout(function () {
$($this).buttonLoader('stop');
}, 3000);
$("#paymentCart_"+a_id).attr("disabled", true);
var id = $('.tour_reference'+a_id).attr('rel');
var name = $('.tour_reference'+a_id).val();
var service_type = $('.service'+a_id).val();
var price = $('.rate'+a_id).attr('rel');
var rfq_id = $('.quote_id'+a_id).val();
var client_id = $('.user_id'+a_id).attr('rel');
var service_id = $('.rfc_id'+a_id).attr('rel');
// var service = service_type.toLowerCase().replace(/[^A-Z0-9]+/ig, "_");
var data = {
id : a_id ,
reference : name,
service_type : service_type,
actual: price,
amount : price,
flag : 0,
rfqId : rfq_id,
clientid : client_id,
serviceId : service_id
}
$.ajax({
type: "POST",
url: absolute1 + 'cart/add_cart',
data: data,
success: function (response) {
addedCart=1;
$.toast({
heading: 'Success',
text: 'Service successfully added in cart',
showHideTransition: 'slide',
icon: 'success',
loaderBg: '#f96868',
position: 'top-right'
});
// $("#paymentCart_"+a_id).attr("disabled", true);
$("#paymentCart_"+a_id).html('Added');
$(".cartcount").text(response);
}
});
}
按钮
<button title="Pay Now" type="button" data-id="<?=$addToCart?>" id="paymentCart_<?=$addToCart;?>" class="btn btn-primary btn-icon btnAddAction refresh-me add-to-cart cart-action has-spinner" style="padding: 0;width:95px; height:30px;" onclick="javascript:advAddtocart(<?=$addToCart;?>)">Add to Cart</button>
解决方案
absolute1变量未定义,在ajax调用之前定义它
并尝试为 ajax 添加错误处理程序
.error(function(e){
console.log(e);
});
推荐阅读
- javascript - vuejs 根据条件选择选项更改
- c# - 嵌套的 CoVariance 会变成逆变吗?
- django - Django 表单向导将数据保存到 PostgreSQL
- java - JaCoCo 未根据源文件生成覆盖率报告 - 方法名称不可点击
- angular - 角度参数没有被隐藏
- java - 如何忽略一个实体中的属性而不忽略另一个实体 - JSON
- c - Gtk3:GtkFixed 中小部件的大小
- c# - 如何在 C# 中使用 SqlFunctions.Checksum 生成散列字符串
- robotframework - RED.xml - 没有为“Library.class.name”库生成库规范文件
- algorithm - 如何随着时间的推移将周期性事件均匀地分布在不同业务的容器中?