這次給大家?guī)?lái)jQuery實(shí)現(xiàn)購(gòu)物車(chē)添加商品并結(jié)算,jQuery實(shí)現(xiàn)購(gòu)物車(chē)添加商品并結(jié)算的注意事項(xiàng)有哪些,下面就是實(shí)戰(zhàn)案例,一起來(lái)看一下。
<!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(); }); /*當(dāng)鼠標(biāo)離開(kāi)文本框時(shí),獲取當(dāng)前值,調(diào)用totalPrice()函數(shù)進(jìn)行結(jié)算*/ $(".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(){ /*循環(huán)購(gòu)物車(chē)列表的每一行*/ var num = parseInt($(this).find(".shopping_product_list_5 input").val()); /*獲取文本框中數(shù)量值*/ var price = parseFloat($(this).find(".shopping_product_list_4 label").text()); /* 獲取商品價(jià)格*/ var total = price * num; allPrice += total; /*計(jì)算所有商品的總價(jià)格*/ /*獲取節(jié)省的金額*/ 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)); /*填寫(xiě)計(jì)算結(jié)果,其中利用toFixed()函數(shù)保留兩位小數(shù)*/ $("#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">您已選購(gòu)以下商品</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">市場(chǎng)價(jià)</td> <tdclass="shopping_list_title_4">當(dāng)當(dāng)價(jià)</td> <tdclass="shopping_list_title_5">數(shù)量</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"> 小團(tuán)圓(張愛(ài)玲最神秘小說(shuō)遺稿)</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">不抱怨的世界(暢銷(xiāo)全球80國(guó)的世界...</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">?,斕仉p桶洗衣機(jī)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開(kāi)發(fā) (原書(shū)第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">法布爾昆蟲(chóng)記</a>(再買(mǎi)¥68.30即可參加“滿199元減10元現(xiàn)金”活動(dòng))</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">清空購(gòu)物車(chē)</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">商品金額總計(jì):</li> <liclass="shopping_list_end_4">您共節(jié)省金額:¥<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>
相信看了本文案例你已經(jīng)掌握了方法,更多精彩請(qǐng)關(guān)注Gxl網(wǎng)其它相關(guān)文章!
推薦閱讀:
jquery插件擴(kuò)展使用詳解
jQuery判斷瀏覽器版本的方法
jQuery表格頂欄固定效果
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com