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

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

    This happens because your input element no longer exist when you close "Open file" dialog window, so there is no target on whom to raise the onchange event. Maybe because JavaScript's garbage collector already collected this element, or by some other reason.

    To fix this just save your input element somewhere in the DOM:

    input.style.visibility='hidden';
    document.body.appendChild(input);
    

    Also don't forget to save link to this element and delete it from DOM when file upload completes (I use here this.set() and this.get() functions from Ext.js):

    // after element initialization:
    this.set("inputFileElement", input);
    ...
    // in the "OnFileComplete" event handler or in some similar place:
    var inputFileElement = this.get("input");
    if(inputFileElement !== null && inputFileElement !== undefined)
    {
        inputFileElement.parentNode.removeChild(inputFileElement);
    }
    

提交回复
热议问题