首页 > 解决方案 > 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>

标签: jquerycodeigniter

解决方案


absolute1变量未定义,在ajax调用之前定义它

并尝试为 ajax 添加错误处理程序

.error(function(e){
   console.log(e);
});


推荐阅读