简体   繁体   English

当聚焦时,如何强制jquery的.change触发文本输入字段上的事件?

[英]How to force jquery's .change to trigger event on text input field when is focused?

I would like to know if my 我想知道我的

<input type="text">

field is changed during edition, not only when it's blured. 字段在编辑期间会更改,不仅在模糊时会更改。 How to achieve this "nicely" using JQuery? 如何使用JQuery做到这一点呢? My best idea is to check on every keydown() event if anything has changed, then trigger ie change() if so. 我最好的主意是检查每个keydown()事件是否有任何更改,然后触发(例如,如果有更改)。 Can this be done better? 可以做得更好吗?

Not really, but I would suggest using keyup, because the value will be 1 keystroke behind at that point. 并非如此,但我建议您使用keyup,因为此时该值将落后1个击键。

Example: 例:

User types: "blah", keydown for the "h" would return the value "bla" because the value hasn't technically changed yet. 用户类型:“ blah”,“ h”的keydown将返回值“ bla”,因为该值在技术上尚未更改。 Plus what if they hold down the key yielding repeating characters? 另外,如果他们按住产生重复字符的键怎么办?

keyup isn't expensive in the browser really if thats what you're worried about. 如果您担心的是,在浏览器中使用keyup并不昂贵。

What are you trying to accomplish exactly and I can edit this with a better example. 您到底想完成什么工作,我可以用一个更好的例子来编辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM