こんにちは、商品一覧ページの商品の購入ボタンをクリックした時、セッションストレージにデータを保存したいのですが、現状のままでは、一つの商品データが入っている状態で、別商品をクリックすると上書きされてしまい、複数商品のデータを保持できません。
何か解決策を教えていただけると幸いです。
jquery
1 function change(genre_id){ 2 $.get(genreid_url+genre_id,function(data){ 3 //html書き換え 4 $('#rewrite').html(""); 5 $(data).each(function products (){ 6 //在庫0売り切れ表記 7 var stockHtml = '<div>' + this.stock + '</div>'; 8 if(this.stock==0){ 9 stockHtml = '<div class=soldOut>' +"売り切れ" + '</div>'; 10 } 11 //値段表記 12 var productPrice = String(this.price); 13 var shapingPrice = productPrice.replace( /(\d)(?=(\d{3})+(?!\d))/g, '$1,' ); 14 //DBから取得したデータを画面表示 15 $('<div class=col-md-4>' + 16 '<img src=../' + this.img_path + ' alt='+this.title+'>' + 17 '<div>' + '【商品名】' + '</div>'+ 18 '<div id="'+ this.title + '">' + this.title + '</div>'+ 19 '<div>' + '【販売元】' + '</div>'+ 20 '<div>' + this.manufacture + '</div>'+ 21 '<div>' + '【在庫数】' + '</div>'+ 22 stockHtml + 23 '<div>' + '【価格】' + '</div>'+ 24 '<div id ="' + this.id + '_price" value=' + this.price + '>' + shapingPrice + "円" + '</div>' + 25 '<a name='+ this.title + ' class="btn btn-default Js-perchaseBtn" data-pechace-price='+ this.price 26 +' data-id=' + this.id + ' data-img-path='+ this.img_path + ' data-manufacture=' + this.manufacture + ' role=button>' + '購入する' + '</a>' + 27 '</div>' ).appendTo('#rewrite') 28 }); 29 }); 30 } 31
セッションストレージの部分
jquery
1$(document).ready(function() { 2 $('#rewrite').on('click', "[data-pechace-price]", function() { 3 alert("カートに追加しました"); 4 var product_tittle = $(this).attr("name"); 5 sessionStorage.setItem("product_tittle",product_tittle); 6 7 var product_price = $(this).attr("data-pechace-price"); 8 sessionStorage.setItem("product_price",product_price); 9 10 var product_id = $(this).attr("data-id"); 11 sessionStorage.setItem("product_id",product_id); 12 13 var img_path = $(this).attr("data-img-path"); 14 sessionStorage.setItem("img_path",img_path); 15 16 var manufacture = $(this).attr("data-manufacture"); 17 sessionStorage.setItem("manufacture",manufacture); 18 }); 19}); 20
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/05/28 01:09