本文實例為大家分享了jQuery實現購物車結算功能展示的具體代碼,供大家參考,具體內容如下
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> <script type="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> <div class="shopping_list_top">您已選購以下商品</div> <div class="shopping_list_border"> <table width="100%" border="1px solid #ccc" > <tr class="shopping_list_title" > <td class="shopping_list_title_1">商品名</td> <td class="shopping_list_title_2">單品積分</td> <td class="shopping_list_title_3">市場價</td> <td class="shopping_list_title_4">當當價</td> <td class="shopping_list_title_5">數量</td> <td class="shopping_list_title_6">刪除</td> </tr> </table> <table width="100%" border="1px solid #ccc" id="myTableProduct"> <tr class="shopping_product_list" id="shoppingProduct_01"> <td class="shopping_product_list_1"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="blue">私募(首部披露資本博弈秘密的金融...</a></td> <td class="shopping_product_list_2"><label>189</label></td> <td class="shopping_product_list_3">¥<label>32.00</label></td> <td class="shopping_product_list_4">¥<label>18.90 </label>(59折)</td> <td class="shopping_product_list_5"><input type="text" value="1"/></td> <td class="shopping_product_list_6"><a href="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> <tr class="shopping_product_list" id="shoppingProduct_02"> <td class="shopping_product_list_1"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="blue"> 小團圓(張愛玲最神秘小說遺稿)</a></td> <td class="shopping_product_list_2"><label>173</label></td> <td class="shopping_product_list_3">¥<label>28.00</label></td> <td class="shopping_product_list_4">¥<label>17.30</label>(62折)</td> <td class="shopping_product_list_5"><input type="text" value="1"/></td> <td class="shopping_product_list_6"><a href="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> <tr class="shopping_product_list" id="shoppingProduct_03"> <td class="shopping_product_list_1"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="blue">不抱怨的世界(暢銷全球80國的世界...</a></td> <td class="shopping_product_list_2"><label>154</label></td> <td class="shopping_product_list_3">¥<label>24.80</label></td> <td class="shopping_product_list_4">¥<label>15.40</label> (62折)</td> <td class="shopping_product_list_5"><input type="text" value="2"/></td> <td class="shopping_product_list_6"><a href="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> <tr class="shopping_product_list" id="shoppingProduct_04"> <td class="shopping_product_list_1"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="blue">福瑪特雙桶洗衣機XPB20-07S</a></td> <td class="shopping_product_list_2"><label>358</label></td> <td class="shopping_product_list_3">¥<label>458.00</label></td> <td class="shopping_product_list_4">¥<label>358.00</label> (78折)</td> <td class="shopping_product_list_5"><input type="text" value="1"/></td> <td class="shopping_product_list_6"><a href="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> <tr class="shopping_product_list" id="shoppingProduct_05"> <td class="shopping_product_list_1"><a href="#" 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> <td class="shopping_product_list_2"><label>712</label></td> <td class="shopping_product_list_3">¥<label>95.00</label></td> <td class="shopping_product_list_4">¥<label>71.20</label> (75折)</td> <td class="shopping_product_list_5"><input type="text" value="1"/></td> <td class="shopping_product_list_6"><a href="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> <tr class="shopping_product_list" id="shoppingProduct_06"> <td class="shopping_product_list_1"><a href="#" 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> <td class="shopping_product_list_2"><label>10</label></td> <td class="shopping_product_list_3">¥<label>198.00</label></td> <td class="shopping_product_list_4">¥<label>130.70</label> (66折)</td> <td class="shopping_product_list_5"><input type="text" value="1"/></td> <td class="shopping_product_list_6"><a href="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> <div class="shopping_list_end"> <div><a id="removeAllProduct" href="javascript:void(0);" rel="external nofollow" >清空購物車</a></div> <ul> <li class="shopping_list_end_1"><input name="" type="image" src="images/shopping_balance.gif"/></li> <li class="shopping_list_end_2">¥<label id="product_total"></label></li> <li class="shopping_list_end_3">商品金額總計:</li> <li class="shopping_list_end_4">您共節省金額:¥<label class="shopping_list_end_yellow" id="product_save"></label><br/> 可獲商品積分:<label class="shopping_list_end_yellow" id="product_integral"></label> </li> </ul> </div> </div> </body></html>以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答