2016-11-23 4 views
0

Ich möchte ausgewählte IDs (unter Verwendung von Guids) in einem Cookie speichern, das später zum Auschecken verwendet wird. Wie speichere ich den neuen Wert in einem Cookie, ohne die alten zu überschreiben?Mehrere Werte zum Cookie mit jQuery hinzufügen

<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') wird Id eines Produkts aus einer foreach-Schleife ausgewählt:

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

Antwort

0

einfach Ihre Cookie lesen und dann hängen Sie mit neuen Werten

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

    }); 
}); 

Verwandte Themen