视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001 知道1 知道21 知道41 知道61 知道81 知道101 知道121 知道141 知道161 知道181 知道201 知道221 知道241 知道261 知道281
问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501
jQuery实现购物车添加商品并结算
2020-11-27 19:53:00 责编:小采
文档

这次给大家带来jQuery实现购物车添加商品并结算,jQuery实现购物车添加商品并结算的注意事项有哪些,下面就是实战案例,一起来看一下。

<!DOCTYPE html>
<html>
 <head>
 <metacharset="UTF-8">
 <title></title>
 <scripttype="text/javascript"src="js/jquery-1.8.3.js"></script>
 <script>
 /*删除*/
 $(function(){
 $(".blue").bind("click",function(){
 $(this).parent().parent().remove();
 totalPrice();
 });
 
 /*当鼠标离开文本框时,获取当前值,调用totalPrice()函数进行结算*/
 $(".shopping_product_list_5 input").bind("blur",function(){
 var t = $(this).val();
 totalPrice();
 });
 var allPrice = 0;
 var allReduce = 0;
 var allCount = 0;
 
 $("#myTableProduct tr").each(function(){ /*循环购物车列表的每一行*/
 var num = parseInt($(this).find(".shopping_product_list_5 input").val()); /*获取文本框中数量值*/
 var price = parseFloat($(this).find(".shopping_product_list_4 label").text()); /* 获取商品价格*/
 var total = price * num;
 allPrice += total; /*计算所有商品的总价格*/
 
 /*获取节省的金额*/
 var reduce = parseFloat($(this).find(".shopping_product_list_3 label").text()) - parseFloat($(this).find(".shopping_product_list_4 label").text());
 var reducePrice = reduce*num;
 allReduce +=reducePrice;
 
 /*获取积分*/
 var count = parseFloat($(this).find(".shopping_product_list_2 label").text());
 allCount +=count;
 });
 $("#product_total").text(allPrice.toFixed(2)); /*填写计算结果,其中利用toFixed()函数保留两位小数*/
 $("#product_save").text(allReduce.toFixed(2));
 $("#product_integral").text(allCount.toFixed(2));
});
 function totalPrice(){
 var allPrice = 0;
 var allReduce = 0;
 var allCount = 0;
 $("#myTableProduct tr").each(function(){
 var num = parseInt($(this).find(".shopping_product_list_5 input").val());
 var price = parseFloat($(this).find(".shopping_product_list_4 label").text());
 var total = price * num;
 allPrice += total;
 
 var reduce = parseFloat($(this).find(".shopping_product_list_3 label").text()) - parseFloat($(this).find(".shopping_product_list_4 label").text());
 var reducePrice = reduce*num;
 allReduce +=reducePrice;
 
 var count = parseFloat($(this).find(".shopping_product_list_2 label").text());
 allCount +=count;
 });
 $("#product_total").text(allPrice.toFixed(2));
 $("#product_save").text(allReduce.toFixed(2));
 $("#product_integral").text(allCount.toFixed(2));
 }
 </script>
 </head>
 <body>
 <pclass="shopping_list_top">您已选购以下商品</p>
 <pclass="shopping_list_border">
 <tablewidth="100%"border="1px solid #ccc">
 <trclass="shopping_list_title">
 <tdclass="shopping_list_title_1">商品名</td>
 <tdclass="shopping_list_title_2">单品积分</td>
 <tdclass="shopping_list_title_3">市场价</td>
 <tdclass="shopping_list_title_4">当当价</td>
 <tdclass="shopping_list_title_5">数量</td>
 <tdclass="shopping_list_title_6">删除</td>
 </tr>
 </table>
 <tablewidth="100%"border="1px solid #ccc"id="myTableProduct">
 
 <trclass="shopping_product_list"id="shoppingProduct_01">
 <tdclass="shopping_product_list_1"><ahref="#"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"class="blue">私募(首部披露资本博弈秘密的金融...</a></td>
 <tdclass="shopping_product_list_2"><label>189</label></td>
 <tdclass="shopping_product_list_3">¥<label>32.00</label></td>
 <tdclass="shopping_product_list_4">¥<label>18.90 </label>(59折)</td>
 <tdclass="shopping_product_list_5"><inputtype="text"value="1"/></td>
 <tdclass="shopping_product_list_6"><ahref="javascript:void(0)"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"class="blue">删除</a></td>
 </tr>
 <trclass="shopping_product_list"id="shoppingProduct_02">
 <tdclass="shopping_product_list_1"><ahref="#"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"class="blue"> 小团圆(张爱玲最神秘小说遗稿)</a></td>
 <tdclass="shopping_product_list_2"><label>173</label></td>
 <tdclass="shopping_product_list_3">¥<label>28.00</label></td>
 <tdclass="shopping_product_list_4">¥<label>17.30</label>(62折)</td>
 <tdclass="shopping_product_list_5"><inputtype="text"value="1"/></td>
 <tdclass="shopping_product_list_6"><ahref="javascript:void(0)"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"class="blue">删除</a></td>
 </tr>
 <trclass="shopping_product_list"id="shoppingProduct_03">
 <tdclass="shopping_product_list_1"><ahref="#"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"class="blue">不抱怨的世界(畅销全球80国的世界...</a></td>
 <tdclass="shopping_product_list_2"><label>154</label></td>
 <tdclass="shopping_product_list_3">¥<label>24.80</label></td>
 <tdclass="shopping_product_list_4">¥<label>15.40</label> (62折)</td>
 <tdclass="shopping_product_list_5"><inputtype="text"value="2"/></td>
 <tdclass="shopping_product_list_6"><ahref="javascript:void(0)"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"class="blue">删除</a></td>
 </tr>
 <trclass="shopping_product_list"id="shoppingProduct_04">
 <tdclass="shopping_product_list_1"><ahref="#"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"class="blue">福玛特双桶洗衣机XPB20-07S</a></td>
 <tdclass="shopping_product_list_2"><label>358</label></td>
 <tdclass="shopping_product_list_3">¥<label>458.00</label></td>
 <tdclass="shopping_product_list_4">¥<label>358.00</label> (78折)</td>
 <tdclass="shopping_product_list_5"><inputtype="text"value="1"/></td>
 <tdclass="shopping_product_list_6"><ahref="javascript:void(0)"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"class="blue">删除</a></td>
 </tr>
 <trclass="shopping_product_list"id="shoppingProduct_05">
 <tdclass="shopping_product_list_1"><ahref="#"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"class="blue">PHP和MySQL Web开发 (原书第4版)</a></td>
 <tdclass="shopping_product_list_2"><label>712</label></td>
 <tdclass="shopping_product_list_3">¥<label>95.00</label></td>
 <tdclass="shopping_product_list_4">¥<label>71.20</label> (75折)</td>
 <tdclass="shopping_product_list_5"><inputtype="text"value="1"/></td>
 <tdclass="shopping_product_list_6"><ahref="javascript:void(0)"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"class="blue">删除</a></td>
 </tr>
 <trclass="shopping_product_list"id="shoppingProduct_06">
 <tdclass="shopping_product_list_1"><ahref="#"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"class="blue">法布尔昆虫记</a>(再买¥68.30即可参加“满199元减10元现金”活动)</td>
 <tdclass="shopping_product_list_2"><label>10</label></td>
 <tdclass="shopping_product_list_3">¥<label>198.00</label></td>
 <tdclass="shopping_product_list_4">¥<label>130.70</label> (66折)</td>
 <tdclass="shopping_product_list_5"><inputtype="text"value="1"/></td>
 <tdclass="shopping_product_list_6"><ahref="javascript:void(0)"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"class="blue">删除</a></td>
 </tr>
 </table>
 <pclass="shopping_list_end">
 <p><aid="removeAllProduct"href="javascript:void(0);"rel="external nofollow">清空购物车</a></p>
 <ul>
 <liclass="shopping_list_end_1"><inputname=""type="image"src="images/shopping_balance.gif"/></li>
 <liclass="shopping_list_end_2">¥<labelid="product_total"></label></li>
 <liclass="shopping_list_end_3">商品金额总计:</li>
 <liclass="shopping_list_end_4">您共节省金额:¥<labelclass="shopping_list_end_yellow"id="product_save"></label><br/>
 可获商品积分:<labelclass="shopping_list_end_yellow"id="product_integral"></label>
 </li>
 </ul>
 </p>
 </p>
 </body>
</html>

相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!

推荐阅读:

jquery插件扩展使用详解

jQuery判断浏览器版本的方法

jQuery表格顶栏固定效果

下载本文
显示全文
专题