doulingna9420 2015-03-10 10:06
浏览 69

如何通过jquery ajax永久更改html元素,以便在刷新页面后保留?

Im a beginner in codeigniter and am trying to make sort of an ecommerce website.

Now whenever a user clicks on the "Add to cart" button in my view, i use jquery ajax to send a request to a controller function which returns two variables count(total items in cart) and total(the total value of items in cart) as a string and then i modify a div with class .cart to simply display a string containing the two variables.

$(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

        }
    });

    });
})

But the string doesnt remain when i refresh the page or go to another page. i want the change made through jquery permanent and remain their regardless where the user navigates in the website. I need direction on how to accomplish this. if i need to save it to my database, then what and how to go about it

  • 写回答

1条回答 默认 最新

  • doulang2311 2015-04-04 08:04
    关注

    use post method like

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

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名