我有以下脚本代码,其中前两个函数和位于底部的第一个变量中的qty是这里最重要的:
// show qty and price
function add(qty) {
qty++;
document.getElementById("qty").innerHTML = (qty);
}
function remove(qty) {
if (qty > 0) {
qty--;
document.getElementById("qty").innerHTML = (qty);
} else {
document.getElementById("qty").innerHTML = (qty);
}
}
// load products on home
function bl() {
document.getElementById("bl").style.display = "initial";
document.getElementById("br").style.display = "none";
}
function br() {
document.getElementById("br").style.display = "initial";
document.getElementById("bl").style.display = "none";
}
// load cart
function cart() {
document.getElementById("cart").style.display="block";
if (qty == 0) {
document.getElementById("by").style.display="none";
} else {
document.getElementById("by").style.display="block";
}
}
// leave cart
function leavecart() {
document.getElementById("cart").style.display="none";
}
// product list
var flower = {
blueyellow: {
color : "linear-gradient(to right, blue, yellow)",
src : "https://cdn.pixabay.com/photo/2022/07/22/15/11/woman-7338352_1280.jpg",
price : 30,
qty : 0,
size : "small"
},
redyellow: {
color : "red, yellow",
src : "https://cdn.pixabay.com/photo/2022/05/22/16/50/outdoors-7213961_1280.jpg",
price : 25,
qty : 0,
size : "large"
}
};
// product list in cart
document.getElementById("bycolor").style.display = (flower.blueyellow.color);
document.getElementById("bysrc").src = (flower.blueyellow.src);
document.getElementById("byprice").innerHTML = ("$" + flower.blueyellow.price);
然后我有两个按钮:一个用于添加到数量,另一个用于从数量中删除:
<div id="wrap" style="padding: 10px;">
<h2>Add to Cart</h2><p id="qty"></p><button onclick="add(flower.blueyellow.qty)">+</button><button onclick="remove(flower.blueyellow.qty)">-</button>
</div>
这里的问题是,onClick只会使数量变为1,而不会进一步增加.在try 使其适应变量中的变量之前,我让它工作,但现在它停止了.