簡體   English   中英

我如何通過jquery ajax對html元素進行永久更改,以使其在刷新頁面后仍保留?

[英]How do i make changes to an html element through jquery ajax permanent so it remains after refreshing the page?

我是Codeigniter的初學者,正在嘗試建立一個電子商務網站。

現在,每當用戶單擊我視圖中的“添加到購物車”按鈕時,我都使用jquery ajax向控制器函數發送請求,該函數返回兩個變量count(購物車中的商品總數)和total(購物車中商品的總價值) )作為字符串,然后我將.cart類的div修改為僅顯示包含兩個變量的字符串。

$(document).ready(function(){

    $("#addtocart").click(function(){


    var id = $("#product_id").val();
    var qty = $("#quantity").val(); 

$.ajax({
        url: "<?php echo base_url('cart/addtocart'); ?>",
        data: {id: id , qty:qty },
        type: 'POST',       
        success: function(name) {

            $(".cart").html(name); //This is the div where show number of items in cart and total value of cart

        }
    });

    });
})

但是當我刷新頁面或轉到另一頁面時,字符串不會保留。 我希望通過jquery所做的更改永久保留,並且無論用戶在網站中的導航位置如何,都保留它們。 我需要有關如何完成此操作的指導。 如果我需要將其保存到數據庫中,那么該如何處理

使用像

$.post('url',{var:value,var2:value},function(data){
  $('.cart').html(data);
});

$('.cart').html(data); or $('.cart').load(data);

暫無
暫無

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

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