基于Javascript實(shí)現(xiàn)頁(yè)面商品個(gè)數(shù)增減功能
效果
后臺(tái)返回代碼
Map<GoodsVO, Integer> cart = new HashMap(); cart.put(new GoodsVO(1001,"蘋(píng)果",100),5); cart.put(new GoodsVO(1002,"桔子",300),3); cart.put(new GoodsVO(1003,"香蕉",150),3); request.getSession().setAttribute("cat",cart);
前端頁(yè)面
<c:forEach items="${cart}" var="item"> <tr> <td>${item.key.name}</td> <td> <span id="subSpan${item.key.id}" class="subSpan">-</span> <input type="text" id="amountInput${item.key.id}" value="${item.value}" style="width: 40px;"> <span id="addSpan${item.key.id}" class="addSpan">+</span> <input type="hidden" id="stockInput" value="${item.key.stock}"> </td> </tr> </c:forEach> <script src="assets/js/jquery3.6.0.js"></script> <script> //購(gòu)買(mǎi)商品數(shù)量減1 $(".subSpan").click(function () { let subSpanId = $(this).attr("id"); let id = subSpanId.substring(7); let amount = $("#amountInput" + id).val(); if (amount - 1 <= 0) { alert("所購(gòu)商品數(shù)量不能小于等于0"); return; } $("#amountInput" + id).val(amount - 1); }) //購(gòu)買(mǎi)商品數(shù)量加1 $(".addSpan").click(function () { let addSpanId = $(this).attr("id"); let id = addSpanId.substring(7); let amount = $("#amountInput" + id).val(); let stock = $("#stockInput").val(); if (amount > stock) { alert("所購(gòu)商品數(shù)量不能大于庫(kù)存"); return; } $("#amountInput" + id).val(amount + 1); }) </script>
實(shí)現(xiàn)二(重點(diǎn))
<div class="jia_jian"> <img class="jian" style="height: 25px;width: 21px;" src="images/jian.jpg"/> <input class="shuliang" type="text" value="1"/> <img class="jia" style="height: 25px;width: 21px;" src="images/jia.jpg"/> <input type="hidden" value="5"> </div> <script type="text/javascript"> //購(gòu)買(mǎi)商品數(shù)量減1 $(".jian").click(function () { let amount = $(this).next().val(); if (amount - 1 <= 0) { alert("所購(gòu)商品數(shù)量不能小于等于0"); return; } $(this).next().val(amount - 1); }) //購(gòu)買(mǎi)商品數(shù)量加1 $(".jia").click(function () { let amount = $(this).prev().val(); let stock = $(this).next().val(); if (amount >= stock || amount>=200) { alert("所購(gòu)商品數(shù)量不能大于庫(kù)存 或 最多只能買(mǎi)200個(gè)"); return; } $(this).prev().val(1+Number(amount)); }) </script>
到此這篇關(guān)于Javascript實(shí)現(xiàn)頁(yè)面商品個(gè)數(shù)增減功能的文章就介紹到這了,更多相關(guān)js商品個(gè)數(shù)增減內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
typescript類(lèi)型體操及關(guān)鍵字使用示例詳解
這篇文章主要為大家介紹了typescript類(lèi)型體操及關(guān)鍵字使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11聯(lián)合類(lèi)型Union?Types與交叉類(lèi)型Intersection?Types區(qū)別解析
這篇文章主要為大家介紹了聯(lián)合類(lèi)型Union?Types與交叉類(lèi)型Intersection?Types區(qū)別詳解2023-06-06rollup?cli開(kāi)發(fā)全面系統(tǒng)性rollup源碼分析
這篇文章主要為大家介紹了rollup?cli開(kāi)發(fā)全網(wǎng)系統(tǒng)性rollup源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01前端構(gòu)建 Less入門(mén)(CSS預(yù)處理器)
眾多CSS預(yù)處理器中Less的語(yǔ)法最接近原生CSS,因此相對(duì)來(lái)說(shuō)更容易上手,假如有JS、C#等編程經(jīng)驗(yàn)的話(huà),其實(shí)上述的幾種預(yù)處理器的學(xué)習(xí)成本也不會(huì)特別高。下面是我們這陣子的學(xué)習(xí)筆記,以便日后查閱2017-03-03數(shù)據(jù)結(jié)構(gòu)TypeScript之鏈表實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了數(shù)據(jù)結(jié)構(gòu)TypeScript之鏈表實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01