简体   繁体   English

使用文件输入进行onchange的解决方法是什么?

[英]What is the workaround for onchange with file inputs?

As some of you may know already, Internet Explorer's onchange event is fundamentally broken prior to version 9. Instead of triggering when a change occurs, it triggers when the input field loses the focus and has changes. 正如你们中的一些人可能已经知道的那样,Internet Explorer的onchange事件在版本9之前从根本上被破坏。而不是在发生更改时触发,它会在输入字段失去焦点并发生更改时触发。

This lead to various workarounds for checkboxes and radio buttons ("use onclick instead") and text fields ("use keyup instead"). 这导致复选框和单选按钮(“使用onclick ”)和文本字段(“使用keyup ”)的各种变通方法。

However, I'm having that problem for a file input, and I can't figure out what I do to be notified that a new file has been selected, right after it did, and not when the user clicks elsewhere. 但是,我遇到了文件输入的问题,而且我无法弄清楚我做了什么以获得通知已经选择了新文件,而不是当用户点击其他地方时。 I can't attach myself to a mouse event because it's not related to the mouse; 我无法将自己附加到鼠标事件中,因为它与鼠标无关; and I can't attach myself to a keyboard event because it's not related to the keyboard either. 我无法将自己附加到键盘事件上,因为它与键盘无关。

I'd be willing to use IE-specific stuff if it can solve the problem. 如果可以解决问题,我愿意使用特定于IE的东西。


Additional infos: 其他信息:

I use jQuery 1.6 and the live method to attach the event. 我使用jQuery 1.6和live方法来附加事件。

$(".upload").live("change", function() { /* stuff here */ });

Use the onFocus event, combined with a check to ensure that there is a value. 使用onFocus事件并结合检查以确保存在值。 This works because after the user selects a file and the OS file selection dialog box is removed, focus is returned to the input element. 这是因为在用户选择文件并删除OS文件选择对话框后,焦点将返回到input元素。

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

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