js 购物车 关闭页面后 重新打开页面 后 显示关闭页面前的商品数量 怎么做

 

$.ajax({

type: "GET",

url: getCar,

data: {

userID: userID1,

},

dataType: "JSONP",

success: function(data) {

if(data == 0) {

console.log("没有搜索结果");

} else {

console.log(data);

for(var i = 0; i < data; i++);

var getCarHtml = '';

var length = data.length;

for(var i = 0; i < data.length; i++) {


var goodsListImg = data[i].goodsListImg;

var goodsName = data[i].goodsName;

var className = data[i].className;

var goodsIDa = data[i].goodsID;

var price = data[i].price;

// var numbers = data[i].number;

// console.log(numbers+"00000000");


getCarHtml += "<li a='" + goodsIDa + "' class='prdListCon-fir'><div class='prdListCon-left'><img class='Img' src='" + goodsListImg + "'/></div><div class='prdListCon-right'><div class='tit1'>" + goodsName + "</div><img class='delete' src='../img/delete.jpg' /><div class='tit2'></div><div class='tit3'>" + className + "</div><div class='tit4'><span>单价 ¥</span><b>" + price + "</b></div><div class='tit5'>数量:</div><div class='tit6 jian'>-</div><div class='tit7'>1</div><div class='tit8 add'>+</div></div></li>"

}

$('#prdListCon').append(getCarHtml);

myScroll = new IScroll('#wrapper', {});

//加的效果

$(".add").on("touchend", function() {

var bs = $(this).parent().parent().attr('a');

//console.log(bs);

var n = $(this).prev().text();

console.log(n+"nnnnn")

var num = parseInt(n) + 1;

if(num == 0) {

return;

}

$(this).prev().text(num);

add_Or_del(updatecars,userID1,bs,num);

totalNum_Mon();


});


//减的效果

$(".jian").on("touchend", function() {

var bs = $(this).parent().parent().attr('a');

var n = $(this).next().text();

var num = parseInt(n) - 1;

if(num == 0) {

return;

}

$(this).next().text(num);

//购物车添加更新函数

add_Or_del(updatecars,userID1,bs,num);

totalNum_Mon();

});


$('.delete').on("touchend", function() {

var bs = $(this).parent().parent().attr('a');

//console.log(bs);

//购物车删除

add_Or_del(updatecars,userID1,bs,0);


//删除节点

$(this).parent().parent().remove();

totalNum_Mon();

});

//时时更新商品总数和总价

totalNum_Mon();

function totalNum_Mon(){

var numTop=0,MonTop=0;

//取出每类商品的数量

$('.tit7').each(function(){

numTop+=parseInt(this.innerHTML);

//console.log(numTop);

//取出每类商品数量的单价

var singlePrice=parseInt($('.tit4 b').eq($('.tit7').index($(this))).text());

/*console.log(numTop)

console.log(singlePrice)*/

//每类商品的数量乘上单价,让和相加

MonTop+=parseInt(this.innerHTML)*singlePrice;

// console.log(MonTop);

});

// console.log(numTop);

$('.shopMes span').html(numTop);

$('.shopMes i').html(MonTop);

}


}

}

})

}

//购物车增加、删减更新函数

function add_Or_del(updatecarUrl,userID,goodsID,nums) {

$.ajax({

type: "GET",

url: updatecarUrl,

data: {

userID: userID,

goodsID: goodsID,

number: nums

},

success: function(data) {

console.log(data)

if(data == 1) {

console.log("更新成功");

} else {

console.log('更新不成功')

}


}

})

}


慕粉13838166718
浏览 1612回答 1
1回答

荒丶謬

可以使用HTML5 LocalStorage 本地存储.
打开App,查看更多内容
随时随地看视频慕课网APP