【问题标题】:Add multiple values to cookie with jQuery使用 jQuery 向 cookie 添加多个值
【发布时间】:2016-11-23 19:48:22
【问题描述】:

我想将选定的 ID(使用 Guids)存储到稍后用于结帐的 cookie 中。如何在不覆盖旧值的情况下将新值存储到 cookie 中?

<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:

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

【问题讨论】:

    标签: jquery asp.net-mvc cookies


    【解决方案1】:

    只需读取您的 cookie,然后在其上附加新值

    <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'));
    
        });
    });
    

    【讨论】:

      猜你喜欢
      • 2017-09-07
      • 1970-01-01
      • 2013-05-30
      • 1970-01-01
      • 2015-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多