jQuery實(shí)現(xiàn)購(gòu)物車全功能
本文實(shí)例為大家分享了jQuery實(shí)現(xiàn)購(gòu)物車全功能的具體代碼,供大家參考,具體內(nèi)容如下
效果圖:
HTML&&CSS:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="../jquery-3.4.1.min.js"></script> <style> * { margin: 0; padding: 0; } .tab { width: 500px; border-collapse: collapse; margin: 0 auto; } .tab td, th { border: 1px solid #000; } .tab .num { width: 20px; } .tab .add, .sub { width: 20px; } .current { background-color: pink; } </style> </head> <body> <table class="tab"> <thead> <th>全選 <input type="checkbox" name="" value="" class="checkAll"> <input type="checkbox" name="" value="" class="checkAll"> </th> <th>商品名稱</th> <th>單價(jià)</th> <th>數(shù)量</th> <th>小計(jì)</th> <th>操作</th> </thead> <tbody> <tr> <td><input type="checkbox" class="ed" /></td> <td>電腦</td> <td class="price">¥200.20</td> <td> <button type="button" class="sub">-</button> <input type="text" name="" value="1" class="num"> <button type="button" class="add">+</button> </td> <td class="small_total">¥200.20</td> <td class="delete">刪除</td> </tr> <tr> <td><input type="checkbox" class="ed" /></td> <td>手機(jī)</td> <td class="price">¥100.30</td> <td> <button type="button" class="sub">-</button> <input type="text" name="" value="1" class="num"> <button type="button" class="add">+</button> </td> <td class="small_total">¥100.30</td> <td class="delete">刪除</td> </tr> <tr> <td><input type="checkbox" class="ed" /></td> <td>空調(diào)</td> <td class="price">¥1000.99</td> <td> <button type="button" class="sub">-</button> <input type="text" name="" value="1" class="num"> <button type="button" class="add">+</button> </td> <td class="small_total">¥1000.99</td> <td class="delete">刪除</td> </tr> </tbody> </table> <div> <span>已選<span style="color: red;" class="num_sum">1</span>件商品</span> <span>總計(jì):</span> <span class="sum" style="color: red;">0</span> <div><span style="color: red;" class="delSome">刪除選中商品</span> <span style="color: red;" class="delAll">清空購(gòu)物車</span> </div> </div> </body> </html>
JS:
//里面三個(gè)小的復(fù)選按鈕選中狀態(tài)跟著 全選按鈕走 //因?yàn)閏hecked是復(fù)選框的固有屬性,此時(shí)利用prop()獲取和設(shè)置該屬性 $(function() { getSum(); $(".checkAll").change(function() { // console.log($(this).prop("checked"));//全選按鈕的狀態(tài) $(".ed,.checkAll").prop("checked", $(this).prop("checked")); getSum(); if ($(".ed,.checkAll").prop("checked")) { //如果全選,讓所有商品添加類名(背景顏色) $(".tab tbody").children().addClass("current"); } else { $(".tab tbody").children().removeClass("current"); } }) //如果所有小按鈕的個(gè)數(shù)都被選了,全選按鈕就選上,如果小按鈕沒(méi)有被選上,則全選按鈕就不選上 //:checked選擇器,查找本選中的表單元素 $(".ed").change(function() { // console.log($(".ed:checked").length);//小復(fù)選框選中的個(gè)數(shù) // console.log($(".ed").length); //console.log($(this).prop("checked")); if ($(".ed:checked").length === $(".ed").length) { $(".checkAll").prop("checked", true); } else { $(".checkAll").prop("checked", false); } getSum(); if ($(this).prop("checked")) { $(this).parents("tr").addClass("current"); } else { $(this).parents("tr").removeClass("current"); } }) $(".add").click(function() { let n = parseInt($(this).siblings(".num").val()); //console.log(n); n++; $(this).siblings(".num").val(n); let price = $(this).parent().siblings(".price").html(); price = price.substr(1); //console.log(price); $(this).parent().siblings(".small_total").text("¥" + (n * price).toFixed(2)); getSum(); }) $(".sub").click(function() { let n = parseInt($(this).siblings(".num").val()); //console.log(n); if (n === 1) { return false; } n--; $(this).siblings(".num").val(n); let price = $(this).parent().siblings(".price").html(); price = price.substr(1); //console.log(price); $(this).parent().siblings(".small_total").text("¥" + (n * price).toFixed(2)); getSum(); }) //用戶也可以直接修改表單num里面的值(小bug),同樣計(jì)算小計(jì) $(".num").change(function() { let n = $(this).val(); let price = $(this).parent().siblings(".price").html(); price = price.substr(1); $(this).parent().siblings(".small_total").text("¥" + (n * price).toFixed(2)); getSum(); }) function getSum() { let count = 0; //計(jì)算總件數(shù) let money = 0; //計(jì)算總價(jià)錢 $(".num").each(function(index) { if ($(".ed").eq(index).prop("checked") == true) { count += parseInt($(".num").eq(index).val()); money += parseFloat($(".small_total").eq(index).text().substr(1)); } }) $(".num_sum").html(count); $(".sum").html(money.toFixed(2)); } //刪除商品模塊 //點(diǎn)擊刪除之后一定是刪除當(dāng)前的商品,所以從$(this)出發(fā) $(".delete").click(function() { //刪除的是當(dāng)前的商品 $(this).parent().remove(); $(".ed").change(); getSum(); clearCheckAll(); }) //刪除選定的商品:小的復(fù)選框如果選中就刪除對(duì)應(yīng)的商品 $(".delSome").click(function() { //刪除的是選中的商品 $(".ed:checked").parent().parent().remove(); getSum(); clearCheckAll(); }) //清空購(gòu)物車 $(".delAll").click(function() { $(".tab tbody").empty(); getSum(); clearCheckAll(); }) function clearCheckAll() { if ($(".tab tbody")[0].innerText == '') { $(".checkAll").prop("checked", false); } } })
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
jQuery實(shí)現(xiàn)在HTML文檔加載完畢后自動(dòng)執(zhí)行某個(gè)事件的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)在HTML文檔加載完畢后自動(dòng)執(zhí)行某個(gè)事件的方法,結(jié)合實(shí)例形式分析了document的ready()事件自動(dòng)加載執(zhí)行事件的相關(guān)操作技巧,需要的朋友可以參考下2017-05-05jquery自定義類似$.ajax()的方法實(shí)現(xiàn)代碼
$.ajax()的方法想必大家都不會(huì)陌生,下面為大家介紹下使用jquery自定義類實(shí)現(xiàn)類似$.ajax()的方法,感興趣的朋友可以參考下2013-08-08jQuery.extend 與 jQuery.fn.extend的用法及區(qū)別實(shí)例分析
這篇文章主要介紹了jQuery.extend 與 jQuery.fn.extend的用法及區(qū)別,結(jié)合實(shí)例形式分析了jQuery.extend與jQuery.fn.extend的功能、使用方法及區(qū)別,需要的朋友可以參考下2018-07-07jquery中l(wèi)oad方法的用法及注意事項(xiàng)說(shuō)明
本篇文章主要是對(duì)jquery中l(wèi)oad方法的用法及注意事項(xiàng)進(jìn)行了詳細(xì)介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-02-02jQuery實(shí)現(xiàn)對(duì)無(wú)序列表的排序功能(附demo源碼下載)
這篇文章主要介紹了jQuery實(shí)現(xiàn)對(duì)無(wú)序列表的排序功能,涉及jQuery與javascript常見(jiàn)的文本操作函數(shù)與sort排序函數(shù)的相關(guān)使用方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06基于jQuery的一個(gè)擴(kuò)展form序列化到j(luò)son對(duì)象
jQuery沒(méi)有直接支持form到j(luò)son的序列化方法,目前網(wǎng)上有一個(gè)實(shí)現(xiàn)是這樣的2010-12-12