简体   繁体   English

不等待输入框失去焦点的Onchange事件

[英]Onchange event that does not wait for input box to lose focus

The onchange event for a HTML input box does not fire until the textbox loses focus. 在文本框失去焦点之前,不会触发HTML输入框的onchange事件。 Is there a way to make it fire as soon as the contents change? 有什么办法可以在内容更改后立即启动?

Here's an example of onchange firing after focus loss: http://jsfiddle.net/McjqW/ 这是失去焦点后触发onchange的示例: http : //jsfiddle.net/McjqW/

You can use onkeyup instead 您可以改用onkeyup

<input type="text" id="fname" onkeyup="upperCase(this.id)">

http://jsfiddle.net/McjqW/1/ http://jsfiddle.net/McjqW/1/

You are looking for the onkeypress or onkeydown event. 您正在寻找onkeypressonkeydown事件。 Note that this event is not standard, and different browsers may expose this functionality under different names. 请注意,此事件不是标准事件,并且不同的浏览器可能以不同的名称公开此功能。 The DOM level 3 standard defines a keypress event which would be "the" answer, but again I can't vouch for browser support. DOM 3级标准定义了一个keypress事件,该事件将是“ the”答案,但是同样,我不能保证浏览器的支持。

Update: onkeypress reportedly enjoys wide support . 更新: 据报道, onkeypress 得到了广泛的支持

You need the HTML5 input event. 您需要HTML5 input事件。 It's not quite fully cross-browser but there's a workaround for IE < 9, which is the only major browser that doesn't have it. 它并不是完全跨浏览器的,但是有一个针对IE <9的解决方法,这是唯一没有此功能的主流浏览器。 See here and here . 看到这里这里

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

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