UPDATED for clarification and example
(更新为澄清和示例)
examples: http://jsfiddle.net/pxfunc/5kpeJ/
(范例: http : //jsfiddle.net/pxfunc/5kpeJ/)
Method 1. input
event
(方法1。 input
事件)
In modern browsers use the input
event.
(在现代浏览器中,使用input
事件。)
This event will fire when the user is typing into a text field, pasting, undoing, basically anytime the value changed from one value to another.(当用户在任何时候将值从一个值更改为另一个值时,都将在用户键入文本字段,粘贴,撤消操作时触发此事件。)
In jQuery do that like this
(在jQuery中这样做)
$('#someInput').bind('input', function() {
$(this).val() // get the current value of the input field.
});
starting with jQuery 1.7, replace bind
with on
:
(从jQuery 1.7开始,用on
替换bind
:)
$('#someInput').on('input', function() {
$(this).val() // get the current value of the input field.
});
Method 2. keyup
event
(方法keyup
事件)
For older browsers use the keyup
event (this will fire once a key on the keyboard has been released, this event can give a sort of false positive because when "w" is released the input value is changed and the keyup
event fires, but also when the "shift" key is released the keyup
event fires but no change has been made to the input.).
(对于较旧的浏览器,请使用keyup
事件(一旦释放键盘上的某个键,就会触发该事件,该事件可能会产生某种误报,因为释放“ w”时,输入值会更改,并且keyup
事件也会触发释放“ Shift”键时,会触发keyup
事件,但未对输入进行任何更改。))
Also this method doesn't fire if the user right-clicks and pastes from the context menu:(如果用户右键单击并从上下文菜单中粘贴,此方法也不会触发:)
$('#someInput').keyup(function() {
$(this).val() // get the current value of the input field.
});
Method 3. Timer ( setInterval
or setTimeout
)
(方法3。计时器( setInterval
或setTimeout
))
To get around the limitations of keyup
you can set a timer to periodically check the value of the input to determine a change in value.
(为了解决keyup
的局限性,您可以设置一个计时器来定期检查输入的值以确定值的变化。)
You can use setInterval
or setTimeout
to do this timer check.(您可以使用setInterval
或setTimeout
进行此计时器检查。)
See the marked answer on this SO question: jQuery textbox change event or see the fiddle for a working example using focus
and blur
events to start and stop the timer for a specific input field(请参阅以下SO问题的标记答案: jQuery文本框更改事件,或查看小提琴,获取使用focus
和blur
事件启动和停止特定输入字段的计时器的工作示例。)