最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery file input on change not triggering - Stack Overflow

programmeradmin0浏览0评论

I have bound two separate events for testing, but for whatever reason neither of them are triggering when I select a file. I'm sure I have just not had enough coffee today but it seems to be the correct way of detecting file selection.

HTML:

<input type="file" id="files">

JS:

$(document).on("ready", function() {

    $("#files").on("change", function() {
        alert("Files changed.");
    });

    $(document).on("change", "#files", function() {
        alert("Files changed.");
    });

});

example: /

I have bound two separate events for testing, but for whatever reason neither of them are triggering when I select a file. I'm sure I have just not had enough coffee today but it seems to be the correct way of detecting file selection.

HTML:

<input type="file" id="files">

JS:

$(document).on("ready", function() {

    $("#files").on("change", function() {
        alert("Files changed.");
    });

    $(document).on("change", "#files", function() {
        alert("Files changed.");
    });

});

example: https://jsfiddle/snoapps/66y45hjh/

Share Improve this question edited Nov 30, 2021 at 20:03 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jan 19, 2016 at 16:12 Micaiah WallaceMicaiah Wallace 1,14610 silver badges17 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

https://jsfiddle/66y45hjh/2/

Your second function was correct but the way the functions are nested wouldn't let it execute the way you wanted it to. Take a look at the edit I made to your fiddle.

The javascript code I used was as follows.

$(function() {
    $(document).on("change", "#files", function() {
        alert("Files changed.");
    });
});
  1. It's $('document').ready(function(){}); https://api.jquery./ready

  2. You don't need your second function

So:

$(document).ready(function() {

    $("#files").on("change", function() {
        alert("Files changed.");
    });


});
发布评论

评论列表(0)

  1. 暂无评论