检测“价值”文本字段中的 keydown 事件后的输入文本字段?
我的网站有一个输入框,其中有一个 onkeydown
事件,仅提醒输入框的值。
不幸的是,输入的值不包括由于按下按键而发生的变化。
例如对于这个输入框:
<input onkeydown="alert(this.value)" type="text" value="cow" />
默认值为“cow”。当您在输入中按下 s 键时,您会收到 alert("cow")
而不是 alert("cows")
。如何在不使用 onkeyup
的情况下实现 alert("cows")
?我不想使用 onkeyup
因为它感觉响应速度较慢。
一种部分解决方案是检测按下的键,然后将其附加到输入的值,但这并不适用于所有情况,例如,如果您突出显示输入中的文本,然后按下某个键。
有人有完整的解决方案来解决这个问题吗?
My site has an input box, which has a onkeydown
event that merely alerts the value of the input box.
Unfortunately the value of the input does not include the changes due to the key being pressed.
For example for this input box:
<input onkeydown="alert(this.value)" type="text" value="cow" />
The default value is "cow". When you press the key s in the input, you get an alert("cow")
instead of an alert("cows")
. How can I make it alert("cows")
without using onkeyup
? I don't want to use onkeyup
because it feels less responsive.
One partial solution is to detect the key pressed and then append it to the input's value, but this doesn't work in all cases such as if you have the text in the input highlighted and then you press a key.
Anyone have a complete solution to this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
注意:自从我写这个答案以来已经过去十多年了(!!)。
input
事件已经变得无处不在,应该使用它来代替这个 hack。keydown/keyup 对于平板电脑和语音输入设备意味着什么?
事件处理程序只能看到应用更改之前的内容,因为
mousedown
和input< /code> events 让您有机会在事件到达现场之前阻止该事件。
您可以通过让浏览器有机会在获取字段值之前更新字段内容来解决此限制 - 最简单的方法是在检查值之前使用较小的超时。
一个最小的示例是:
这设置了 1 毫秒的超时,该超时应该在按键和按键处理程序让控件更改其值之后发生。如果您的显示器以 60 fps 刷新,那么在滞后 2 帧之前您有 16 毫秒的回旋空间。
更完整的示例(不依赖于 对 Window 对象的命名访问 如下所示:
运行上面的代码片段时,请尝试执行以下操作:
NOTE: It's over a decade (!!) since I wrote this answer. The
input
event has become ubiquitous, and should be used instead of this hack.What does keydown/keyup even mean for tablet and voice input devices?
The event handler only sees the content before the change is applied, because the
mousedown
andinput
events give you a chance to block the event before it gets to the field.You can work around this limitation by giving the browser a chance to update the field's contents before grabbing its value - the simplest way is to use a small timeout before checking the value.
A minimal example is:
This sets a 1ms timeout that should happen after the keypress and keydown handlers have let the control change its value. If your monitor is refreshing at 60fps then you've got 16ms of wiggle room before it lags 2 frames.
A more complete example (which doesn't rely on named access on the Window object would look like:
When you run the above snippet, try some of the following:
请尝试使用oninput事件。
与onkeydown不同,onkeypress事件此事件更新控件的值属性。
Please, try to use oninput event.
Unlike onkeydown, onkeypress events this event updates control's value property.
请注意,在较新的浏览器中,您将能够使用新的 HTML5“输入”事件 (https://developer.mozilla.org/en-US/docs/DOM/window.oninput)。大多数非IE浏览器长期以来都支持该事件(请参阅链接中的兼容性表);对于 IE 来说,不幸的是它的版本是 >/9。
Note that in newer browsers you'll be able to use the new HTML5 "input" event (https://developer.mozilla.org/en-US/docs/DOM/window.oninput) for this. Most non-IE browsers have supported this event for a long time (see compatibility table in the link); for IE it's version >/9 only unfortunately.
keyup/down 事件在不同的浏览器中处理方式不同。简单的解决方案是使用像 mootools 这样的库,这将使它们的行为相同,处理传播和冒泡,并在回调中为您提供标准的“this”。
keyup/down events are handled differently in different browsers. The simple solution is to use a library like mootools, which will make them behave the same, deal with propagation and bubbling, and give you a standard "this" in the callback.
据我所知,除非您自己推出,否则您无法使用标准输入控件来做到这一点。
To my knowledge you can't do that with a standard input control unless you roll your own.
Jquery 是执行此操作的最佳方法。请参考以下内容:
Jquery is the optimal way of doing this. Please reference the following below: