admin管理员组文章数量:1291062
I'm relying on the change event being triggered whenever the contents of the textarea changes. According to the jquery docs, this doesn't get triggered until the textarea loses focus. Under what circumstances could the event not be triggered? Would clsoing a page, going back, clicking on a link, etc, always cause the textarea to lose focus and be fired?
Or to put it another way, is it possible to leave a page without a focusses textarea losing input?
I'm relying on the change event being triggered whenever the contents of the textarea changes. According to the jquery docs, this doesn't get triggered until the textarea loses focus. Under what circumstances could the event not be triggered? Would clsoing a page, going back, clicking on a link, etc, always cause the textarea to lose focus and be fired?
Or to put it another way, is it possible to leave a page without a focusses textarea losing input?
Share Improve this question edited Jan 19, 2012 at 7:09 Paul Biggar asked Jan 19, 2012 at 6:21 Paul BiggarPaul Biggar 28.8k24 gold badges103 silver badges155 bronze badges 2- Must be be just the change event? There are plenty of other events; e.g. keyUp, keyDown, keyPress, focus, paste. – Wayne Commented Jan 19, 2012 at 7:03
- Because change is the event I'm using :) – Paul Biggar Commented Jan 19, 2012 at 7:08
5 Answers
Reset to default 3With differents tests, the change event is not triggered when:
- the user clicks on any button of the browser (back button, refresh, home button...) (Chrome, Firefox amd Opera on Linux but triggered when click on adress bar)
- the user uses shortcuts (f5, ctrl+u...) (tested on Chrome + Firefox + Opera).
You can test an example here: http://jsfiddle/Atinux/S6TkP/
So I remand you to use in addition keyUp and keyDown events on your input and textarea forms.
I would go to the W3c definition of the event.
[http://www.w3/TR/DOM-Level-2-Events/events.html][1]
The change event occurs when a control loses the input focus and its value has been modified since gaining focus. This event is valid for
INPUT
,SELECT
, andTEXTAREA
element.
- Bubbles: Yes
- Cancelable: No
- Context Info: None
Any time it loses focus, it should be fired.
On Safari and Chrome whenever you change the text the CHANGE event is fired. However, if you delete all the text then the CHANGE event is not fired anymore. This is a bug. This is why people use the KEYPRESS events as a workaround
The event will be triggered but you won't be able to listen to it reliably. Form elements lose focus when a page is unloading but there is a race condition around when the even gets triggered and when the listeners get called. If your listener is called before the page is done unloading, you might get the callback but it's far from guaranteed.
If you want to be able to keep track of changes to a textarea
more reliably you are better off listening to the keypress
event.
Its better to use keyUp/keyDown event cause the change event only fires when the textare/input was changed and the element lost its focus.
本文标签: javascriptWhen do textarea change events not get triggeredStack Overflow
版权声明:本文标题:javascript - When do textarea change events not get triggered? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741504992a2382269.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论