所以我想添加一個產品,同時更新購物車中的總價格。如何使用javascript顯示和更新總價格?
$(".articel input[type='button']").click(function() {
var price = $(this).siblings("input[name='price']").attr("value");
var quantity = $(this).siblings("input[type='number']").attr("value");
if (quantity % 1 != 0) {
alert("You must add a whole number");
}
else if (quantity <= 0) {
alert("You can not add a negative number or nothing");
}
else {
var name = $(this).siblings("input[name='prodname']").attr("value");
var ul = document.getElementById("buylist");
var totalprice = quantity * price;
var prod = name + " x " + quantity + "= " + totalprice + "$";
var el = document.createElement("li");
el.innerHTML = prod;
ul.appendChild(el);
}
});
});
這裏就是產品和totalprice補充說:
<h4>Shopping Cart</h4>
<div id="buylist">
<ul>
</ul>
<div id="totalprice">
<h4>Total price:<h4>
</div>
</div>
<a href="#checkout" onClick="checkOut()" class="click" data-toggle="tab" id="form">Checkout</a>
</div>
在這裏,在那裏我將產品添加到購物車
<form class="articel">
Quantity: <input type="number" style="width:30px;"><br>
Add to cart: <input type="button" class="btn">
<input type="hidden" value="30" name="price">
<input type="hidden" value="The walking dead" name="prodname">
</form>
那麼是什麼問題? – Chad
所以它增加了一個產品和該產品的總價格。問題是,當我想添加一件產品時,購物車中所有產品的總價應該出現在購物車中產品的底部。有沒有簡單的方法來做到這一點? (或不簡單...) – JJeff