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
Add a ment  | 

2 Answers 2

Reset to default 4

I 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