背景:在做一个购物车的时候,点击第一个商品,然后存入一个json数组中,点击第二个商品的时候,又继续在json数组中追加,代码如下:
<script type="text/javascript"> var jsonArray = { "goods": [] }; //点击商品进入cookie function addCookie(objName, objValue, objDays) {//添加cookie $.cookie(objName, objValue, { expires: objDays }); var cookieList = { id: objName, price: objValue }; jsonArray.goods.push(cookieList); console.log(jsonArray); //console.log(js.id); } </script>
我这里是假如了cookie的。看看效果图吧,最终实现的结果:
右击查看图片可以看大图哈,谅解哈,图片大了。