简体   繁体   中英

Any alternative to jQuery change() to detect when user selects new file via dialog box in IE8?

I am unable to detect when input type="file" changes its value after user selects file and the dialog box closes.

$('.myInput').change(function(){
    alert($(this).val());
})

Above jQuery code works perfectly in all browsers apart from IE. For some reason IE detects the change only after input field loses focus.

Is there a way to detect the change immediately after dialog box closes? Or maybe to force input field to lose focus after dialog box closes so IE can detect it?

I'm puzzled. Thanks for any help.

是一个已知的错误 ,作为jQuery 1.4.2版本的一部分得到解决,1.4.2得到了一个重要的事件模型重写 ,这是固定的一部分,只是升级来解决问题:)

Edit - Nick is right, it's fixed in 1.4.2. http://jsfiddle.net/7wR2L/

You can detect click and keep track of it's last value. Something like..

$('.myInput').click(function() {
   var $file = $(this);
   if( $file.val() != $file.data('lastVal') ) {
     // different
   }
   $file.data('lastVal', $file.val() );
});

Dan Heberden's comment about updating to 1.4.2 works.

However if another element is used to trigger the file file selection, the file input element no longer registers a "change" event.

The new question I created for this has a fork your fiddle to illustrate this case. See jQuery: "change" event on file input element does not fire if the file selection is triggered by an element other than the file input for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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