java web购物网站的购物车。用jQuery实现怎么样才能就写一段代码然后遍历输出商品信息,而不是多一个商品就多写一段代码?
这是具体的输出计算代码,他这边是一个商品就得写一次这个table然后还要一段js。怎么样只写一段就行了?
全选 | 商品 | 商品信息 | 单价(元) | 数量 | 单价 | 操作 |
<!---商品加减算总数---->
$(function () {
var t = $("#text_box1");
$("#add1").click(function () {
t.val(parseInt(t.val()) + 1)
setTotal(); GetCount();
})
$("#min1").click(function () {
t.val(parseInt(t.val()) - 1)
setTotal(); GetCount();
})
function setTotal() {
$("#total1").html((parseInt(t.val()) * document.getElementById("goodsprice").innerHTML).toFixed(2));
$("#newslist-1").val(parseInt(t.val()) * document.getElementById("goodsprice").innerHTML);
}
setTotal();
})
<c:forEach var="mm" items="${cart.map}">
<table cellpadding="0" cellspacing="0" class="gwc_tb2">
<tr>
<td class="tb2_td1"><input type="checkbox" value="1" name="newslist" id="newslist-1" /></td>
<td class="tb2_td2"><a href="#"><img src="images/img1.jpg"/></a></td>
<td class="tb2_td3"><a href="#">${mm.value.goods.goods_name }</a></td>
<td class="tb1_td4">${mm.value.goods.description }</td>
<td class="tb1_td8" id="goodsprice">${mm.value.goods.price }</td>
<td class="tb1_td5">
<input id="min1" name="" style=" width:20px; height:18px;border:1px solid #ccc;" type="button" value="-" onclick="add()"/>
<input id="text_box1" name="" type="text" value="${mm.value.amount}" style=" width:30px; text-align:center; border:1px solid #ccc;" />
<input id="add1" name="" style=" width:20px; height:18px;border:1px solid #ccc;" type="button" value="+" />
</td>
<td class="tb1_td6"><label id="total1" class="tot" style="color:#ff5500;font-size:14px; font-weight:bold;"></label></td>
<td class="tb1_td7"><a href="${pageContext.request.contextPath }/DeleteShoppingCartServlet?goods_id=${mm.value.goods.goods_id}">删除</a></td>
</tr>
</table>
</c:forEach>
<!---商品加减算总数---->
<!-- <script type="text/javascript">
$(function () {
var t = $("#text_box2");
$("#add2").click(function () {
t.val(parseInt(t.val()) + 1)
setTotal(); GetCount();
})
$("#min2").click(function () {
t.val(parseInt(t.val()) - 1)
setTotal(); GetCount();
})
function setTotal() {
$("#total2").html((parseInt(t.val()) * 8).toFixed(2));
$("#newslist-2").val(parseInt(t.val()) * 8);
}
setTotal();
}) -->
</script>
<!-- <table cellpadding="0" cellspacing="0" class="gwc_tb2">
<tr>
<td class="tb2_td1"><input type="checkbox" value="1" name="newslist" id="newslist-2" /></td>
<td class="tb2_td2"><a href="#"><img src="images/img2.jpg"/></a></td>
<td class="tb2_td3"><a href="#">产品标题</a></td>
<td class="tb1_td4">一件</td>
<td class="tb1_td5">
<input id="min2" name="" style=" width:20px; height:18px;border:1px solid #ccc;" type="button" value="-" />
<input id="text_box2" name="" type="text" value="1" style=" width:30px; text-align:center; border:1px solid #ccc;" />
<input id="add2" name="" style=" width:20px; height:18px;border:1px solid #ccc;" type="button" value="+" />
</td>
<td class="tb1_td6"><label id="total2" class="tot" style="color:#ff5500;font-size:14px; font-weight:bold;"></label></td>
<td class="tb1_td7"><a href="#">删除</a></td>
</tr>
</table> -->