Event onChange won't trigger after files are selected from code-generated INPUT element

前端 未结 5 822
执笔经年
执笔经年 2021-01-17 18:00

I\'m playing with JavaScript and wrote simple function that creates INPUT element (type=\"file\") and simulates click.

var createAn         


        
5条回答
  •  既然无缘
    2021-01-17 18:32

    I know this question was not about a workaround, but I came here looking for a solution. This worked for me on Chrome. I have simply moved let input outside the function. This works because (as suggested by bside) it prevents input from being garbage collected. I only expect to have one open-file dialog open at a time so the singleton pattern is OK here.

    let input;
    var createAndCallFileSelect = function () {
        input = document.createElement ("input");
        input.setAttribute ("type", "file");
        input.addEventListener ("change", function () {
            console.log (this.files);
        }, false);
        input.click();
    }
    

提交回复
热议问题