Jquery:将事件更改为 IE 上的输入文件 [英] Jquery: change event to input file on IE

查看:23
本文介绍了Jquery:将事件更改为 IE 上的输入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个上传文件的表单,它应该在文件选择后触发提交.

I have a form to upload files, and it should fire the submit after the file selection.

在 FireFox/Chrome 上运行良好,并在选择文件后提交表单,但我无法在 Internet Explorer 上执行此操作.

On FireFox/Chrome it goes well, and submits the form after file selection, but I can't do this on Internet Explorer.

已经尝试过单击/属性更改,但没有任何反应.我已经尝试过的一些代码:

Already tried with click/propertychange but nothing happens. Some code I already tried:

$("#attach").attr("onChange", "alert('I changed')");

$("#attach").live($.browser.msie? 'propertychange': 'change', function(e) { ... });

这个输入文件是动态创建的;因为它我使用 .live() 来绑定事件.

This input file is created on the fly; because of it I use .live() to bind the event.

有什么建议吗?

推荐答案

我知道这已经晚了几个月,但我在 IE7 中遇到了完全相同的行为;在所有其他浏览器中,文件输入的更改事件发生在文件选择之后.在 IE7 中,只有再次触发文件选择或模糊时才会发生这种情况.

I know this is several months late, but I just ran into the exact same behavior in IE7; in all other browsers, the change event for file inputs happens after file selection. In IE7, it happens only if you trigger the file select again, or on blur.

这是我最终修复它的方式:

Here's how I ended up fixing it:

var $input = $('#your-file-input-element');

var someFunction = function()
{
    // what you actually want to do
};

if ($.browser.msie)
{
    // IE suspends timeouts until after the file dialog closes
    $input.click(function(event)
    {
        setTimeout(function()
        {
            if($input.val().length > 0) {
              someFunction();
            }
        }, 0);
    });
}
else
{
    // All other browsers behave
    $input.change(someFunction);
}

从技术上讲,您可以/应该将 hack 条件过滤为仅 IE7,因为 IE8 在更改事件上的行为正确,但它也具有与 IE7 相同的行为,即在与浏览器相关的 chrome 可见时暂停超时(我猜它认为它阻塞 I/O),所以它按原样工作.

Technically you could/should filter the hack condition to just IE7, since IE8 behaves properly on the change event, but it also has the same behavior as IE7 on suspending timeouts while browser-related chrome is visible (I guess it considers it blocking I/O), so it works as-is.

这篇关于Jquery:将事件更改为 IE 上的输入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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