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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

购物车逻辑

發布時間:2025/3/12 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 购物车逻辑 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

當我們添加購物車之后,我們可以點擊加減按鈕來實現商品數量的增加和減少,當商品數量變化的同時商品的總的價格也是在不停的變化的。

效果圖

完整代碼

<!DOCTYPE html> <html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title></title><script src="https://code.jquery.com/jquery-3.4.1.min.js"></script></head><body><p>商品數量: <span onclick="numDec()">-</span> <input type="text" id="quantity" value="1" /> <span onclick="numAdd()">+</span></p><p class="sdsd">商品價格: <span id="totalPrice">28.10</span></p><input type="hidden" value="28.1" id="price" /></body><script type="text/javascript">function keyup(){var quantity = document.getElementById("quantity").value;if(isNaN(quantity) || parseInt(quantity)!=quantity || parseInt(quantity)<1){quantity = 1;return;}if(quantity>=10){document.getElementById("quantity").value=quantity.substring(0,quantity.length-1);alert("商品數量不能大于10");}} function numAdd(){var quantity = document.getElementById("quantity").value;var num_add = parseInt(quantity)+1;var price=document.getElementById("price").value;if(quantity==""){num_add = 1;}if(num_add>=10){document.getElementById("quantity").value=num_add-1;alert("商品數量不能大于10");}else{document.getElementById("quantity").value=num_add;var Num=price*num_add;document.getElementById("totalPrice").innerHTML=Num.toFixed(2);}}/*商品數量-1*/function numDec(){var quantity = document.getElementById("quantity").value;var price=document.getElementById("price").value;var num_dec = parseInt(quantity)-1;if(num_dec>0){document.getElementById("quantity").value=num_dec;var Num=price*num_dec;document.getElementById("totalPrice").innerHTML=Num.toFixed(2);}}</script> </html>

總結

以上是生活随笔為你收集整理的购物车逻辑的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。