我有添加到购物车的按钮,单击该按钮时我想更新我的购物车, 添加新产品(也更新计数器++),但是使用此代码,它只会插入最后点击的产品,并删除其他产品。以及是否有可能从购物车中删除产品
<script>
$(document).ready(function () {
$('.add_to_cart').click(function () {
var product_id = $(this).data('id');
var product_name = $(this).data('name');
var product_price = $(this).data('price');
$.ajax({
url: "/uketesi/index",
method: "POST",
datatype: "json",
data: {
'product_id':product_id,
'product_name':product_name,
'product_price':product_price,
},
success:(function (data) {
alert("produqti warmatebit daemata")
$("#cart").html("<table id=\"example2\">" +
"<thead>" +
"<tr>" +
"</tr>"+
"<tr>" +
"<td>" + product_name + "</td>" +
"<td>" + product_price + "</td>" +
"<td>" + product_id + "</td>" +
"</tr>" +
"</thead>" +
"</table>");
})
});
});
});
</script>
答案 0 :(得分:4)
使用此:
$(document).ready(function () {
$('.add_to_cart').click(function () {
var product_id = $(this).data('id');
var product_name = $(this).data('name');
var product_price = $(this).data('price');
$.ajax({
url: "/uketesi/index",
method: "POST",
datatype: "json",
data: {
'product_id':product_id,
'product_name':product_name,
'product_price':product_price,
},
success:(function (data) {
alert("produqti warmatebit daemata")
$("#cart table tbody").append(
"<tr>" +
"<td>" + product_name + "</td>" +
"<td>" + product_price + "</td>" +
"<td>" + product_id + "</td>" +
"</tr>");
int counter = $("#cart table tbody tr").length;
})
});
});
});