localstorage购车(对象)所用到函数
function added(){ var data = JSON.parse(localStorage.getItem('yaop')); var yao_name = $('.yao_name').html(); var yao_cd = $('.yao_cd').html(); var yao_cl = $('.yao_cl').html(); var yao_zb = $('.yao_zb').html(); var yao_db = $('.yao_db').html(); var yao_gg = $('.yao_gg').html(); var yao_ph = $('.yao_ph').html(); var yao_time = $('.yao_time').html(); var yao_price = $('.yao_price').html(); var yao_dw = $('.yao_dw').html(); var num_box = $('.num_box').val(); var id = $("input[name='yid']").val(); var img = $('.myimg').prop('src'); value = { 'yao_name':yao_name, 'yao_cd':yao_cd, 'yao_cl':yao_cl, 'yao_zb':yao_zb, 'yao_db':yao_db, 'yao_gg':yao_gg, 'yao_ph':yao_ph, 'yao_price':yao_price, 'yao_time':yao_time, 'yao_dw':yao_dw, 'num_box':num_box, 'id':id, 'img':img }; if(data !=""&&data !=null&&data !=undefined && data !="undefined" && JSON.stringify(data)!="{}"){ data[id]=value; }else{ data={}; data[id]=value; } localStorage.setItem('yaop',JSON.stringify(data)); getData(); } function getData(){ var nn = $('.cart-num'); var rs = JSON.parse(localStorage.getItem('yaop')); var html=""; var len = 0; for(var i in rs){ len++; var a = rs[i].yao_name; var b = rs[i].num_box; var c = rs[i].img; var d = rs[i].id; html += '<div>'+'<img src="'+c+'" alt="" width="50" height="50">' +'<span class="add_car_title">'+a+'</span>' +'<span class="add_car_nums">'+'数量:'+b+'</span>' +'<span class="add_car_remove" onclick="delData('+d+')">删除</span>' +'</div>'; } nn.html(len); $('.yesgoods').html(html); if(len==0){ $(".nogoods").css("display","block"); }else{ $(".nogoods").css("display","none"); } } getData(); function delData(id){ var data=JSON.parse(localStorage.getItem("yaop")); delete data[id]; localStorage.setItem("yaop",JSON.stringify(data)); getData(); llist(); }