我正在尝试创建增加或减少数量值的按钮。
HTML:
<div class="order-option">
Quantity:
<span id="quantity-field">
<button id="up" onclick="setQuantity('up');">+</button>
<input type="text" id="quantity" value="1">
<button id="down" onclick="setQuantity('down');">-</button>
</span>
</div>
JavaScript:
function setQuantity(upordown) {
var quantity = document.getElementById('quantity');
if (quantity.value > 1) {
if (upordown == 'up'){++document.getElementById('quantity').value;}
else if (upordown == 'down'){--document.getElementById('quantity').value;}}
else if (quantity.value == 1) {
if (upordown == 'up'){++document.getElementById('quantity').value;}}
else
{document.getElementById('quantity').value=1;}
}
它非常干燥。该函数根据单击的按钮“向上”或“向下”传递,然后根据数量元素的当前值决定要做什么。不幸的是,它没有做任何事情,我不知道为什么。任何帮助将非常感激。