使用jQuery向cookie添加多个值 [英] Add multiple values to cookie with jQuery

查看:160
本文介绍了使用jQuery向cookie添加多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将选定的ID(使用Guids)存储到以后用于结帐的Cookie中。如何将新值存储到cookie而不覆盖旧值?

I would like to store selected Id's(using Guids) into a cookie that will later be used for checkout. How do I store the new value into a cookie without overwrite the old ones?

<script type="text/javascript">
    $(document).ready(function () {
        $('.AddProduct').click(function () {

        //add following: $(this).data('id'), this is how far I got:
        //$.cookie("AddedProductId", $(this).data('id'));

        });
    });
</script>

$(this).data('id')从foreach循环中选择产品的ID:

$(this).data('id') gets selected Id of a product from a foreach loop:

<button class="AddProduct" data-ID="@item.ID">Add to Cart</button>


推荐答案

只要读取您的Cookie,

Just read your cookie and then append it with new values

<script type="text/javascript">
$(document).ready(function () {
    $('.AddProduct').click(function () {

    //add following: $(this).data('id'), this is how far I got:
    var previousValues = $.cookie("AddedProductId", $(this).data('id'));
    $.cookie("AddedProductId", previousValues+' ' + $(this).data('id'));

    });
});

这篇关于使用jQuery向cookie添加多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆