Jquery: change event to input file on IE

2019-01-03 06:59发布

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.

On FF/Chrome it goes weel, and submit the form after file selection, but I can't do this on ie.

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) { ... });

Any sugestions to I try?

Edit1: 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

15条回答
我只想做你的唯一
2楼-- · 2019-01-03 07:01

This is really late, but I was having the same problem, and I solved it by using a styled <label> tag with a slight workaround for Firefox.

http://jsfiddle.net/djibouti33/uP7A9/

The Goals:

  1. allow user to upload a file by using standard html file input control
  2. hide standard html file input control and apply own styling
  3. after user selects file to upload, automatically submit the form

The Browsers:

  • Firefox, Chrome, IE8/9, Safari
  • IE7 didn't work, but it might if you add that browser to the workaround detailed at the bottom

The Initial Solution:

  1. Hide the file input by positioning it offscreen. Important not to display:none as some browsers won't like this.
  2. Add another styled element to the page (link, button).
  3. Listen for a click on that element, then programmatically send a click to the file input to trigger the native 'file explorer'
  4. Listen for the file input's onchange event (occurs after a user chooses their file)
  5. Submit the form

The Problem:

  1. IE: if you programmatically send a click to a file input in order to activate it (2), programmatically submitting the form (5) will throw a security error

The Workaround Solution:

  1. Same as above
  2. Take advantage of the accessibility features built in to the tag (clicking on a label will activate it's associated control) by styling a tag instead of a link/button
  3. Listen for the file input's onchange event
  4. Submit the form
  5. For some reason Mozilla browsers won't activate a file input by clicking on it's .
  6. For Mozilla, listen for the click on the label and send a click event to the file input to activate it.

Hope this helps! Check out the jsfiddle for details on the html/js/css used to make it all work.

查看更多
唯我独甜
3楼-- · 2019-01-03 07:03

I was having the same issue with IE (including IE 9). The UI logic is:

  1. click on a div element triggers the click event on a file-input-element so that user click on a div trigger file open dialog
  2. bind the change event handler to the file-input-element to ensure the form is submitted when file open dialog closed

The app (running inside an iframe) works like a charm on Chrome and FF. But soon I found it doesn't work on IE as when user selected a file and close the dialog the form didn't submit.

The final solution is to drop the logic #1 "click on div element trigger click event on file input element" and let user to click on the file input element directly, and then it works.

BTW, the reason we have a div element is we want to hide the browser rendered controls because we have everything in the background image. However set display to none makes the control not able to respond a user interaction event. So we move the file-input-element to outside of the view port and use a div element to replace it. Since this doesn't work on IE, we end up with move back the file-input-element and set it's opacity to 0. On IE 8 or less which doesn't support opacity, we use filter to make it transparent:

#browse {
    filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=0);
    filter: alpha(opacity=0);
    -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
}
查看更多
放荡不羁爱自由
4楼-- · 2019-01-03 07:05

This has always worked for me in IE6 ad IE7.

$('#id-of-input-type-file').change(function(){});
查看更多
虎瘦雄心在
5楼-- · 2019-01-03 07:07
$("#attach").attr("onChange", "alert('I changed')");

It works in IE, but if you want to emulate "live" behavior, you should add "onChange" attribute to each new element when create its.

查看更多
我只想做你的唯一
6楼-- · 2019-01-03 07:12

In IE onchange event works fine when it filled out in html tag directly. Like:

<input type="file" onchange="uploader.upload()" />
查看更多
你好瞎i
7楼-- · 2019-01-03 07:12

For IE You can use the "onfocus" event to catch the change of uploading file. Once the file browsing dialog is closed the onfocus event is triggered. You can check this by adding this line to your page:

<input type="file" onfocus="javascript:alert('test');" />

Once the dialog is closed the alert message is shown.

This solution is only for IE, not for FF or Chrome.

查看更多
登录 后发表回答