简体   繁体   中英

Jquery change event not working

I have html:

<form id="fileuploadform">
    <input type="file" id="fileupload" name="fileupload" />
</form>

and jquery:

$(':file').change(function(){
    var file = this.files[0];
    ...
});

The problem is: the change function fires only when the file selection is different than the selection made previously. That is of course the meaning of the 'change' event. I want my function to be called with the file dialog closes, no matter what.

I tried using 'select' event -- it never gets called

I also tried:

$(':file').bind('dialogclose', function(event) {
    alert('closed');
});

and:

$(':file').live("dialogclose", function(){
    alert('closed1');
}); 

They didn't work either.

I have faced the same issues when I used to work with jQuery dynamic appended HTML.

I would suggest you to use the below solution

$(document).on('input','#fileupload', function () {
  console.log('Your Code')
}); 

try to use this code.

$('#fileupload').on('change', function () {
    alert('Hi');
    //do your function.
});

this function will call only you select different files otherwise not.

I know this is an old post, but try using 'input' instead of 'change'. Worked for me. :)

$('#fileupload').on('input', function () {
  alert('Hi');
});

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