Jquery change event not working

Cod*_*ant 5 html javascript jquery

I have html:

<form id="fileuploadform">
    <input type="file" id="fileupload" name="fileupload" />
</form>
Run Code Online (Sandbox Code Playgroud)

and jquery:

$(':file').change(function(){
    var file = this.files[0];
    ...
});
Run Code Online (Sandbox Code Playgroud)

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.

我尝试使用 'select' 事件——它永远不会被调用

我也试过:

$(':file').bind('dialogclose', function(event) {
    alert('closed');
});
Run Code Online (Sandbox Code Playgroud)

和:

$(':file').live("dialogclose", function(){
    alert('closed1');
}); 
Run Code Online (Sandbox Code Playgroud)

他们也没有工作。

Uma*_*kar 9

当我使用 jQuery 动态附加 HTML 时,我遇到了同样的问题。

我建议您使用以下解决方案

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

  • 这是我脱离地狱的门票。 (2认同)

San*_*etS 6

尝试使用此代码。

$('#fileupload').on('change', function () {
    alert('Hi');
    //do your function.
});
Run Code Online (Sandbox Code Playgroud)

这个函数只会调用你选择不同的文件,否则不会。


Iva*_*own 6

我知道这是一篇旧帖子,但请尝试使用“输入”而不是“更改”。为我工作。:)

$('#fileupload').on('input', function () {
  alert('Hi');
});
Run Code Online (Sandbox Code Playgroud)