2016-11-23 9 views
0

後でチェックアウトに使用するクッキーに、選択したId(Guidsを使用)を保存したいとします。古い値を上書きせずに新しい値をクッキーに保存するにはどうすればよいですか?jQueryで複数の値をクッキーに追加

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

答えて

0

ちょうどあなたのクッキーを読み、新しい値

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

    }); 
}); 

でそれを追加
関連する問題