Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

javascript - 在jQuery中检测输入更改?(Detecting input change in jQuery?)

When using jquery .change on an input the event will only be fired when the input loses focus

(在input上使用jquery .change ,仅当输入失去焦点时才会触发事件)

In my case, I need to make a call to the service (check if value is valid) as soon as the input value is changed.

(就我而言,我需要在输入值更改后立即调用该服务(检查值是否有效)。)

How could I accomplish this?

(我该怎么做?)

  ask by Banshee translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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。计时器( setIntervalsetTimeout)

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.

(您可以使用setIntervalsetTimeout进行此计时器检查。)

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文本框更改事件,或查看小提琴,获取使用focusblur事件启动和停止特定输入字段的计时器的工作示例。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...