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

前端 未结 5 791
执笔经年
执笔经年 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:19

    You can do something like this to trigger click on change of dynamically created file input

    var input = document.createElement ("input");
    input.setAttribute ("type", "file");
    
    input.addEventListener('change', function(){
        input.addEventListener('click', function(){
          alert("Clicked");
          input.removeEventListener("click", function(){})
        }, false);
        input.click();
    }, false); 
    

    JS fiddle

    I have tested this in chrome, firefox, opera nd IE. It works

提交回复
热议问题