admin管理员组文章数量:1389865
I'm trying to create buttons which increments or decrements a quantity value.
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;}
}
It's pretty cut and dry. The function is passed 'up' or 'down' depending on which button is clicked, then decides what to do based on the current value of the quantity element. Unfortunately, it's not doing a thing, and I can't figure out why. Any help would be much appreciated.
I'm trying to create buttons which increments or decrements a quantity value.
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;}
}
It's pretty cut and dry. The function is passed 'up' or 'down' depending on which button is clicked, then decides what to do based on the current value of the quantity element. Unfortunately, it's not doing a thing, and I can't figure out why. Any help would be much appreciated.
Share Improve this question asked Feb 7, 2013 at 22:03 Todd BauerTodd Bauer 2191 gold badge7 silver badges15 bronze badges 1- 4 Can we see a Fiddle? – Mooseman Commented Feb 7, 2013 at 22:04
2 Answers
Reset to default 4I went ahead and pasted the code into a fiddle, and got the console error that at the time of onclick, setQuantity
was not defined. Making sure that the function is declared before the markup that calls it solves the problem for me:
http://jsfiddle/KR2Az/
As alluded to by crowjonah, your javascript should ideally appear in the <HEAD>
of the page.
I also suggest separating the javascript from your HTML like this:
<script>
quantity = document.getElementById('quantity');
button_up=document.getElementById('up');
button_down=document.getElementById('down');
button_up.onclick=function() {setQuantity('up');}
button_down.onclick=function() {setQuantity('down');}
function setQuantity(upordown) {
var quantity = document.getElementById('quantity');
if (quantity.value > 1) {
if (upordown == 'up'){++quantity.value;}
else if (upordown == 'down'){--quantity.value;}}
else if (quantity.value == 1) {
if (upordown == 'up'){++quantity.value;}}
else
{quantity.value=1;}
}
</script>
<div class="order-option">
Quantity:
<span id="quantity-field">
<button id="up">+</button>
<input type="text" id="quantity" value="1">
<button id="down">-</button>
</span>
</div>
jFiddle here
本文标签: HTMLJavaScriptUp and Down buttons for quantity not workingStack Overflow
版权声明:本文标题:HTMLJavaScript - Up and Down buttons for quantity not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744655460a2617936.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论