gpt4 book ai didi

javascript - HTML/JavaScript - 数量的向上和向下按钮不起作用

转载 作者:行者123 更新时间:2023-11-28 13:45:19 26 4
gpt4 key购买 nike

我正在尝试创建增加或减少数量值的按钮。

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;}
}

它非常干燥。该函数根据单击的按钮“向上”或“向下”传递,然后根据数量元素的当前值决定要执行的操作。不幸的是,它没有做任何事情,我不明白为什么。任何帮助将不胜感激。

最佳答案

我继续将代码粘贴到 fiddle 中,并收到控制台错误,在 onclick 时,setQuantity 未定义。确保在调用该函数的标记之前声明该函数可以解决我的问题: http://jsfiddle.net/KR2Az/

关于javascript - HTML/JavaScript - 数量的向上和向下按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14761885/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com