当我使用 javascript 按键盘上的箭头键时增加数字
我有一个文本框有一个数值。 现在我想要的是在按住任何箭头键的同时不断增加该数值。 如果我只按一次,我知道该怎么做。它只会增加 1。但是如果我想在按住箭头键的同时继续增加值该怎么办?怎么办?
谢谢
I have a textbox has a numeric value.
now what I want is to keep increasing that numeric value while im pressing and holding any of arrow keys.
I know how to do this if I was pressing only one time. it will be increased by 1 only. but what If I want to keep increasing the value while i'm holding the arrow keys. how to do that?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有一个小的 jQuery 插件可以做到这一点:
https://github.com/nakupanda/number-updown
用法:
$('#step' ).updown({
步骤:10,
移动步长:100
});
$('#minMax').updown({
分钟:-10,
最多:10
});
$('#minMaxCircle').updown({
分钟:-10,
最大:10,
圆圈:真实
});
在这里查看现场演示:
http://jsfiddle.net/XCtaH/embedded/result/
支持键盘和鼠标滚轮事件
There's a small jQuery plugin for doing this:
https://github.com/nakupanda/number-updown
Usages:
$('#step').updown({
step: 10,
shiftStep: 100
});
$('#minMax').updown({
min: -10,
max: 10
});
$('#minMaxCircle').updown({
min: -10,
max: 10,
circle: true
});
View live demo here:
http://jsfiddle.net/XCtaH/embedded/result/
Keyboard and mousewheel events supporte
我尚未对此进行充分尝试和测试,但这里有一个想法 - 您可能想要跟踪 KeyDown 事件,因为这是第一次按下按键时操作系统排队的事件。您可能还希望在以这种方式递增时实现某种延迟,以免压垮客户端脚本并使数字以很高的速度变化以供用户跟踪。
This is not fully tried and tested by me, but here is a thought - You might want to track KeyDown events because that's the event which is queued by the OS when the key is first pressed. You might also want to implement some sort of delay when incrementing this way so as not to overwhelm the client-side script and have numbers change at a speed to high for user to track.
好的,经过我在这里进行的一些测试后,它是如何完成的:
ok after some tests I made here is how its done:
如果您不关心支持 Opera,这很简单:
但是,Opera 不会针对按键重复触发
keydown
...您必须通过调用incrementTextBox() 来模仿它
每隔一段时间,并在抬起钥匙时停止。我在 WebKit (Chrome 6.0)、FF3、Opera 10.6、IE7、IE8、IE9,甚至 IE Quirks 中对此进行了测试:If you don't care about supporting Opera, this is easy:
However, Opera doesn't fire
keydown
for key repeats... you'll have to mimic that by callingincrementTextBox()
at an interval, and stopping when the key is lifted. I tested this in WebKit (Chrome 6.0), FF3, Opera 10.6, IE7, IE8, IE9, even IE Quirks:我想这样做,我只是使用了 type=“number” 的输入字段
I wanted to do this, i just used input field with type="number"