thymeleaf-+按钮:
前端:
html:
<td style="vertical-align:middle" th:text="${product.price}"></td> <td style="vertical-align:middle"> <span style="margin:5px;padding-left:5px;padding-right: 5px;border:1.5px solid #ccc;" th:onclick="|javascript:jian(this,${product.id})|">-</span> <span th:text="${product.quantity}"></span> <span style="margin:5px;padding-left:5px;padding-right: 5px;border:1.5px solid #ccc;" th:onclick="|javascript:jia(this,${product.id})|">+</span> </td>
js:
//增加: function jia(obj,productId) { var button =$(obj);//表示当前按钮 var numButton=button.prev(); //获取购物车商品总额: var sumPricePre = button.parent().parent().children("td").eq(3); var sumPrice = sumPricePre[0].textContent; sumPrice = parseInt(sumPrice); var num=numButton.text(); num=parseInt(num); //与数据库通信: //发送‘+‘的请求 $.ajax({ url:"/addShop/"+productId, success:function(data){ if(data.trim()=="failed"){ alert("操作失败!"); }else{ num++; data = parseInt(data); sumPrice+=data; sumPricePre[0].textContent = sumPrice; numButton.text(num); } } }); } //减少: function jian(obj,productId){ var button=$(obj); var numButton=button.next(); var num=numButton.text(); num=parseInt(num); //获取购物车商品总额: var sumPricePre = button.parent().parent().children("td").eq(3); var sumPrice = sumPricePre[0].textContent; sumPrice=parseInt(sumPrice); //得到单件商品的价格: var price = sumPrice/num; if(num==1){ return; } $.ajax({ url:"/reduceShop/"+productId, success:function(data){ if(data.trim()=="failed"){ alert("操作失败!"); }else{ num--; sumPrice-=price; sumPricePre[0].textContent = sumPrice; numButton.text(num); } } }); }
原文地址:https://www.cnblogs.com/lyjblogg/p/12230607.html
时间: 2024-10-15 09:32:06