[英]Deleting items from JQuery based only shopping cart
我試圖建立一個JQuery購物車,僅用於鍛煉和學習的借口。 但我在編碼從購物車中刪除商品的選項時遇到了問題。 這是代碼:
<script src="assets/jquery-2.1.0.js" type="text/javascript"></script>
<script type="text/javascript">
var sum=0;
var num=1;
$(function() {
//Assign the producets a price date
$( "#Product1" ).data( "price", 400 );
$( "#Product2" ).data( "price", 500 );
$( "#Product3" ).data( "price", 600 );
//Adding the requested producted to the shopping cart
$(".buy").on("click",function(evt) {
var id=$(this).attr("id");
$("#shopping_cart_items").append($(this).attr( "id" ) + " " + "price: ");
$("#shopping_cart_items").append($(this).data( "price" ) + "$" + "<img src='assets/x.png' width='10px' height='10px' class='cancel' id="
+id + " + data-num= "+ num++ + " + >" + "<br>");
//summing up the overall price
sum=sum+$(this).data( "price" );
$("#sum").text(sum+ "$");
//Delete product from the shopping cart
$(".cancel").on("click",function(evt) {
//?
});
});
});
</script>
<style>
#product img{
width:300px;
height:200px;
}
#product{
float: left;
padding:5px;
}
</style>
<h2>My shop</h2>
<div id="product"><img src="assets/Product1.jpg" /><br /><input type="button" value="Buy" class="buy" id="Product1" /> price:400$</div>
<div id="product"><img src="assets/Product2.jpg" /><br /><input type="button" value="Buy" class="buy" id="Product2"/> price:500$</div>
<div id="product"><img src="assets/Product3.jpg" /><br /><input type="button" value="Buy" class="buy" id="Product3" /> price:600$</div><br />
<div id="shopping_cart" align="center">
<h1>Shopping cart</h1>
<hr />
<div id="shopping_cart_items">
</div>
<hr />
Sum:<div id="sum">0$</div>
</div>
為了將其從購物車中刪除,我給每個產品指定了唯一的編號,並且我知道我可能需要使用.remove()
,但是我嘗試了很多次,但均無法完成。
您必須將購物車產品包裝在一個元素中,以便以后將其刪除。 此外,您將刪除代碼放置在錯誤的位置,必須位於購買click
功能之外>
看看我的演示: http : //jsfiddle.net/GBF7m/
var sum = 0;
var num = 1;
$(function () {
//Assign the producets a price date
$("#Product1").data("price", 400);
$("#Product2").data("price", 500);
$("#Product3").data("price", 600);
//Adding the requested producted to the shopping cart
$(".buy").on("click", function (evt) {
var id = $(this).attr("id");
$("<span/>", {
html: $(this).attr("id") + " " + "price: " + $(this).data("price") + "$" + "<img src='http://placehold.it/20x20' width='20px' height='20px' class='cancel' id=" + id + " + data-num= " + num+++" + ><br />",
"data-price": $(this).data("price")
}).appendTo("#shopping_cart_items");
//summing up the overall price
sum = sum + $(this).data("price");
$("#sum").text(sum + "$");
});
//Delete product from the shopping cart
$("#shopping_cart_items").on("click", ".cancel", function (evt) {
$(this).parent("span").remove();
sum = sum - $(this).parent("span").data("price");
$("#sum").text(sum + "$");
});
});
試試這個: http : //jsfiddle.net/tonicboy/68yyr/
var sum = 0;
var num = 1;
//Assign the producets a price date
$("#Product1").data("price", 400);
$("#Product2").data("price", 500);
$("#Product3").data("price", 600);
//Adding the requested producted to the shopping cart
$(".buy").on("click", function (evt) {
var id = $(this).attr("id"),
item = $('<div class="cart-item"></div>');
item.append($(this).attr("id") + " " + "price: ");
item.append($(this).data("price") + "$" + "<img src='assets/x.png' width='10px' height='10px' class='cancel' id=" + id + " + data-num= " + num++ + " data-price='" + $(this).data("price") + "' >" + "<br>");
item.appendTo($("#shopping_cart_items"));
//summing up the overall price
sum = sum + $(this).data("price");
$("#sum").text(sum + "$");
});
//Delete product from the shopping cart
$("#shopping_cart").on("click", ".cancel", function (evt) {
var $this = $(evt.currentTarget),
cartItem = $this.parent(),
newPrice = parseInt($this.data('price'));
cartItem.remove();
sum = sum - newPrice;
$("#sum").text(sum + "$");
});
更改說明:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.