日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

jQuery实现PC端商城购物车模块基本功能(每个商品的小计和合计都会根据添加和删除的操作来动态计算)

發(fā)布時間:2024/7/5 编程问答 56 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jQuery实现PC端商城购物车模块基本功能(每个商品的小计和合计都会根据添加和删除的操作来动态计算) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

jQuery實現(xiàn)PC端商城購物車模塊基本功能

先上效果圖:
因為主要是想練習(xí)jQuery的使用,所以頁面CSS部分比較簡陋,有需要的話,大家在參考代碼時,可以自己再完善下CSS部分的代碼,讓購物車頁面更加美觀。

功能清單如下:
1.全選非全選商品
2.一鍵刪除選中商品
3.一鍵清空購物車
4.根據(jù)添加和刪除動態(tài)計算已選中的商品總數(shù)和總價格,并渲染到頁面中
5.點擊加減單個商品的數(shù)量,鍵盤輸入改變單個商品的數(shù)量

<!DOCTYPE html> <html><head><meta charset="utf-8"><title>購物車商品數(shù)量增減</title><style type="text/css">a {text-decoration: none;}.itxt {width: 50px;text-align: center;}.clearfix {content: "";visibility: none;display: block;clear: both;}.goods {box-sizing: border-box;/* width: 700px; */height: 100px;border: 1px solid black;padding: 25px;}.g_name {width: 10%;float: left;}.left,.right,.number {float: left;}.left {width: 20%;white-space: nowrap;}.number {width: 30%;text-align: center;}.right {width: 30%;white-space: nowrap;text-align: left;}.delete {text-align: right;display: inline-block;width: 70px;}.goods_check {float: left;height: 20px;}.check_cart_item {background-color: #fff4e8;}</style><script src="js/jquery-3.5.0.js"></script><script type="text/javascript">$(function() {console.log($('.price'));$('.decrease').click(function() {// 1.點擊減號,input框中的數(shù)值也要進行遞減,但要保證input框中的數(shù)值 >= 0var curVal = $(this).siblings('.itxt').val();if (curVal >= 2) {curVal -= 1;$(this).siblings('.itxt').val(curVal);}// 2.獲取當(dāng)前商品的單價,和數(shù)量,相乘得到商品小計的數(shù)值(需要截取字符串,去掉單價里的$符號,并且最終計算得到的商品小計需要保留兩位小數(shù),并把計算結(jié)果更新到頁面的小計中),var price = $(this).parent().siblings('.left').children('.price').html();price = price.substr(1); // 單價var sum = "¥" + (curVal * price).toFixed(2); // 小計$(this).parent().siblings('.right').children('.sum').html(sum);getSum();})$('.increase').click(function() {// 只能獲取當(dāng)前點擊元素的兄弟元素的文本框的值var curVal = $(this).siblings('.itxt').val();curVal = parseInt(curVal);curVal += 1;// 只能改變當(dāng)前點擊元素的兄弟元素的文本框的值,不能改變其他商品的文本框的值$(this).siblings('.itxt').val(curVal);var price = $(this).parent().siblings('.left').children('.price').html();price = price.substr(1); // 單價從第一位截取,來去掉$符號var sum = (curVal * price).toFixed(2); // 小計保留兩位小數(shù)sum = "¥" + sum;$(this).parent().siblings('.right').children('.sum').html(sum);getSum();})// 3.用戶修改文本框的數(shù)值,商品小計數(shù)值也要更新$('.itxt').change(function() {var price = $(this).parent().siblings('.left').children('.price').html();price = price.substr(1); // 單價從第一位截取,來去掉$符號var sum = $(this).val() * price;sum = "¥" + sum.toFixed(2);$(this).parent().siblings('.right').children('.sum').html(sum);getSum();})renew(); // 頁面加載完成后調(diào)用renew()函數(shù),把每個商品的單價的數(shù)值賦值給這個商品的小計// renew()函數(shù)必須在getSum()函數(shù)之前,不然商品總價會是0.00getSum(); //沒有在input中修改值或者沒有點擊加減數(shù)量時,也要調(diào)用這個函數(shù)來將商品總數(shù)量和商品總價進行更新 // 4. 計算總計總額模塊function getSum() {var count = 0; //總件數(shù)var money = 0; //總金額$('.itxt').each(function(i, ele) {count += parseInt($(ele).val());})$('.sumNum em').html(count);$('.sum').each(function(i, ele) {money += parseFloat($(ele).html().substr(1));// console.log(money.toFixed(2))// console.log($(ele).html())})// console.log(money.toFixed(2))$('.sumPrice em').html(money.toFixed(2));}// 頁面加載完成后調(diào)用renew()函數(shù),把每個商品的單價的數(shù)值賦值給這個商品的小計function renew() {var array_price = [];// console.log($('.goods').length);for (var i = 0; i < $('.goods').length; i++) {console.log(parseFloat($('.price')[i].innerHTML.substr(1)).toFixed(2));// array_price.push($('.price')[i].innerHTML); array_price.push(parseFloat($('.price')[i].innerHTML.substr(1)).toFixed(2));}console.log(array_price);$('.sum').each(function(i, ele) {$(ele).html('¥' + array_price[i]);})}// 5.刪除商品模塊// 1)每個商品后面的刪除功能$('.delete').click(function() {$(this).parents('.goods').remove();getSum();})// 2)點擊刪除選中的商品功能$('.removes').click(function() {$('.goods_check:checked').parents('.goods').remove();getSum();})// 3)點擊清空購物車$('.remove_cart').click(function() {$('.goods').remove();getSum();})// 全選 非全選按鈕$('.checkAll').change(function() {$('.goods_check,.checkAll').prop("checked", $(this).prop("checked")) // 將全選按鈕1的 checked的值賦給goods按鈕和另一個全選按鈕2// console.log()if($('.goods_check,.checkAll').prop("checked")) {$('.goods').addClass('check_cart_item');}else {$('.goods').removeClass('check_cart_item');}})$('.goods_check').change(function() {if($(this).prop('checked')){$(this).parents('.goods').addClass('check_cart_item');}else {$(this).parents('.goods').removeClass('check_cart_item');}// :checked選擇器可以幫我們自動獲取已勾選的按鈕,通過length屬性可以獲取已勾選按鈕的數(shù)量 將其 與按鈕的總數(shù)進行對比 來判斷是否所有商品按鈕都已經(jīng)被勾選if ($(".goods_check:checked").length === $('.goods_check').length) {$('.checkAll').prop("checked", true)} else {$('.checkAll').prop("checked", false)}})})</script></head><body>全選<input type="checkbox" name="" id="" value="全選1" class="checkAll" /><div class="goods clearfix"><input type="checkbox" name="" id="" value="商品1" class="goods_check" /><span class="g_name">商品1</span><div class="left"><span>單價:</span><span class="price">12.6</span></div><div class="number"><a href="#" class="decrease">-</a><input type="text" value="1" class="itxt" /><a href="#" class="increase">+</a></div><div class="right"><span>小計:</span><span class="sum">0.00</span><span class="delete"><a href="#">刪除</a></span></div></div><div class="goods clearfix"><input type="checkbox" name="" id="" value="商品1" class="goods_check" /><span class="g_name">商品2</span><div class="left"><span>單價:</span><span class="price">102.9</span></div><div class="number"><a href="#" class="decrease">-</a><input type="text" value="1" class="itxt" /><a href="#" class="increase">+</a></div><div class="right"><span>小計:</span><span class="sum">0.00</span><span class="delete"><a href="#">刪除</a></span></div></div><div class="goods clearfix"><input type="checkbox" name="" id="" value="商品1" class="goods_check" /><span class="g_name">商品3</span><div class="left"><span>單價:</span><span class="price">19.9</span></div><div class="number"><a href="#" class="decrease">-</a><input type="text" value="1" class="itxt" /><a href="#" class="increase">+</a></div><div class="right"><span>小計:</span><span class="sum">0.00</span><span class="delete"><a href="#">刪除</a></span></div></div><div class="goods clearfix"><input type="checkbox" name="" id="" value="商品1" class="goods_check" /><span class="g_name">商品4</span><div class="left"><span>單價</span><span class="price">358.9</span></div><div class="number"><a href="#" class="decrease">-</a><input type="text" value="1" class="itxt" /><a href="#" class="increase">+</a></div><div class="right"><span>小計:</span><span class="sum">0.00</span><span class="delete"><a href="#">刪除</a></span></div></div><div class="bottom">全選<input type="checkbox" name="" id="" value="全選1" class="checkAll" /><span><a href="#" class="removes">刪除選中的商品</a></span><span><a href="#" class="remove_cart">清空購物車</a></span><div class="sumNum">已經(jīng)選中<em>1</em>件商品</div><div class="sumPrice">總計:<em>0.00</em></div></div></body> </html>

總結(jié)

以上是生活随笔為你收集整理的jQuery实现PC端商城购物车模块基本功能(每个商品的小计和合计都会根据添加和删除的操作来动态计算)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。