开发者

Incrementing value (integer) in an input type field

开发者 https://www.devze.com 2023-04-12 18:29 出处:网络
I have input text and I want to increase the number that is inside using the key up or down. I have: <input type=\"text\" value=\"1\" name=\"qty\" />

I have input text and I want to increase the number that is inside using the key up or down.

I have:

<input type="text" value="1" name="qty" />

After digging stackoverflow I found: Is it possible to handle up/down key in HTML input field?

The solution is to use: Keycodes 37 - 40 should do it for you. They map as 37=left, 38=up, 39=right, 40=down.

But How can I use these code in my form? Is there a Javascript function that do this (like : increase() or d开发者_StackOverflowecrease()?

Thanks


You could do something like this, using the "onkeydown" event.

<script type="text/javascript">
function increment(e,field) {
    var keynum

    if(window.event) {// IE 
        keynum = e.keyCode
    } else if(e.which) {// Netscape/Firefox/Opera 
        keynum = e.which
    }
    if (keynum == 38) {
        field.value = parseInt(field.value)+ 1;
    } else if (keynum == 40) {
        field.value = parseInt(field.value) - 1;
    }
    return false;
}
</script>


<input type="text" onkeydown="increment(event, this)" value="10">
0

精彩评论

暂无评论...
验证码 换一张
取 消