模板
user/cart.html
列表
{foreach $carts as $v} {if($v['info'])} <li class="product product-{$v['tid']}-{$v['info']['id']}"> <div class="order-item"> <div class="img-box"> <a href="{fun gourl($v['info']['id'],$v['info']['htmlurl'])}"><img src="{$v['info']['litpic']}" ></a> </div> <div class="order-title"> <a href="{fun gourl($v['info']['id'],$v['info']['htmlurl'])}" target="_blank">{$v['info']['title']}</a> <!--根据不同的字段可以利用 $v['info']输出详情--> </div> </div> <div class="order-item-right"> <div class="order-number"> <p>数量</p> <p><input type="number" class="quantity-input" data-id="{$v['id']}" data-tid="{$v['tid']}" value="{$v['num']}"></p> </div> <input id="price_per_{$v['tid']}_{$v['info']['id']}" value="{$v['info']['price']}" type="hidden" /> <div class="order-money"> <p>单价</p> <p class="price">¥{$v['info']['price']}</p> </div> </div> <div class="col-sm-12 col-md-1"> <button class="btn btn-danger" onclick="delcart({$v['tid']},{$v['info']['id']})" type="button">删除</button> </div> </li> {else} <li> <div class="order-item"> <div class="img-box"> <a >[ 无 ]</a> </div> <div class="order-title"> <h4><a >[{$v['id']}][ 商品已被删除 ]</a></h4> </div> </div> <div class="order-item-right"> <div class="order-number"> <p>数量</p> <p><input type="number" class="quantity-input" data-id="{$v['info']['id']}" data-tid="{$v['tid']}" value="{$v['num']}"></p> </div> <div class="order-money"> <p>单价</p> <p class="price">¥{$v['price']}</p> </div> </div> </li> {/if} {/foreach}
加入购物车
function addcart(tid,id,num){ $.ajax({ url:"/user/addcart",//请求的url地址 dataType:"json",//返回格式为json async:true,//请求是否异步,默认为异步,这也是ajax重要特性 data:{tid:tid,id:id,num:num,ajax:1},//参数值 type:"POST",//请求方式 beforeSend:function(){ //请求前的处理 }, success:function(r){ if(r.code==0){ window.location.href=r.url; }else{ layer.alert(r.msg); } }, complete:function(){ //请求完成的处理 }, error:function(){ //请求出错处理 layer.alert('网络错误'); } }) }