在input type =“text”中跟踪onchange的最佳方式? [英] Best way to track onchange as-you-type in input type="text"?

查看:124
本文介绍了在input type =“text”中跟踪onchange的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的经验, input type =text onchange 事件通常只会在离开后发生( blur )控件。

有没有办法强制浏览器触发 onchange 每次 textfield 内容变化?如果不是,那么手动跟踪这种最优雅的方式是什么?



使用 onkey * 事件不是可靠的,因为你可以右键单击该字段并选择粘贴,这将更改该字段,而无需任何键盘输入。



setTimeout 唯一的办法是......丑陋的: - )

解决方案

em>



请参阅另一个答案(2015)。




Original 2009答案:

因此,您需要 onchange 事件在keydown上开火,模糊,粘贴?这是神奇的。



如果您想在输入时跟踪更改,请使用onkeydown。如果您需要使用鼠标捕获粘贴操作,请使用onpaste IE FF3 )和oninput FF,Opera,Chrome,Safari 1 )。

1 Broken for Safari上的< textarea> 。改为使用 textInput


In my experience, input type="text" onchange event usually occurs only after you leave (blur) the control.

Is there a way to force browser to trigger onchange every time textfield content changes? If not, what is the most elegant way to track this "manually"?

Using onkey* events is not reliable, since you can right-click the field and choose Paste, and this will change the field without any keyboard input.

Is setTimeout the only way?.. Ugly :-)

解决方案

Update:

See Another answer (2015).


Original 2009 Answer:

So, you want the onchange event to fire on keydown, blur, and paste? That's magic.

If you want to track changes as they type, use "onkeydown". If you need to trap paste operations with the mouse, use "onpaste" (IE, FF3) and "oninput" (FF, Opera, Chrome, Safari1).

1Broken for <textarea> on Safari. Use textInput instead

这篇关于在input type =“text”中跟踪onchange的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆