文本输入事件处理程序
我有一个文本输入和一个处理程序例程 testFunc(input.value)
testFunc 应该在以下情况下调用:
- 当按下字母键时
- ,当使用更新值按下退格键时(即,当初始输入为 1234 时,当从末尾按下退格键时,输入对于 testFunc 例程来说应该是 123)
- 如果输入是 1234,并且从中间的某个位置(即 1234)按下退格键,那么 testfunc 应该被调用为测试函数(234)。
- 2、3 对于 DELETE 键也应该类似。
我尝试使用 onkeypress 和 onkeyup,但是 testFunc 处理程序例程没有获得更新的值(或) testFunc 例程本身没有被调用。
谁能告诉我如何解决这个问题或者Javascript中是否有任何现有的API
I have a text input and a handler routine testFunc(input.value)
testFunc should be called when
- When a alphabet key is pressed
- when backspace is hit with updated value (i.e when initial input is 1234, when backspace is hit from the end, input should be 123 to the testFunc routine)
- If the input is 1234, and backspace is hit from somewhere in the middle i.e 1234, then the testfunc should be called as testFunc(234).
- 2, 3 should be similar for DELETE key too.
I tried with onkeypress and onkeyup, but some how testFunc handler routine is not getting with the updated value (or) testFunc routine itself is not getting called.
Can any one let me know how i can resolve this issue or whether there is any existing APIs in Javascript
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 HTML5
input
事件,该事件在输入值更改时触发,并在值更改后触发。与 IE 解决方案相结合,它的兼容性令人惊讶。请参阅以下答案:jQuery 键盘事件
You can use the HTML5
input
event, which is fired whenever the input value changes, and fires after the value has changed. In combination with an IE workaround, it's surprisingly compatible. See the following answer:jQuery keyboard events
在哪些浏览器中?每当按下某个键时,以下内容始终显示输入的当前值:
In which browsers? The following always shows the current value of the input whenever a key is pressed: