Jquery:将事件更改为IE上的输入文件 [英] Jquery: change event to input file on IE
问题描述
我已经看了四周,找不到解决方案:我有一个表单上传文件,应该在文件选择后触发提交。
I already looked all around, and can't find a solution: I have a form to upload files, and it should fire the submit after the file selection.
在FF / Chrome上,它会被删除,并在文件选择后提交表单,但是我不能这样做。
On FF/Chrome it goes weel, and submit the form after file selection, but I can't do this on ie.
已经尝试过click / propertychange,但没有发生。一些我已经尝试的代码:
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){...});
我尝试的任何东西?
编辑1:我认为有一个重要的信息,这个输入文件是即时创建的,因为我使用.live()来绑定事件
I think there's a important information, 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.
以下是我如何修复它:
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);
}
技术上,您可以/应该将黑客条件过滤到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屋!