簡體   English   中英

ajax成功后如何“添加到購物車”按鈕更改文本“已添加”

[英]How to "Add to Cart" button change text "Added" after ajax success

我是新的 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);
});

您正在嘗試使用類“.cartcount”更改按鈕的文本,但按鈕類中沒有這樣的“.cartcount”類。 為什么不使用按鈕的 id?

$('#btn-id').text('TEXT HERE');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM