javascript Jquery 更改事件不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16673834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:35:18  来源:igfitidea点击:

Jquery change event not working

javascriptjqueryhtml

提问by Codegiant

I have html:

我有 html:

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

and jquery:

和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.

他们也没有工作。

采纳答案by SanketS

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.

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

回答by Ivan Town

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

我知道这是一篇旧帖子,但请尝试使用“输入”而不是“更改”。对我来说有效。:)

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

回答by Umashankar

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

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

I would suggest you to use the below solution

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

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