jQuery trigger file input

后端 未结 21 1888
梦如初夏
梦如初夏 2020-11-22 08:49

Am trying to trigger an upload box (browse button) using jQuery.
The method I have tried now is:

$(\'#fileinput\').trigger(\'click\');   
21条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 09:20

    Just for the sake of curiosity, you can do something like you want by dynamically creating an upload form and input file, without adding it to the DOM tree:

    $('.your-button').on('click', function() {
        var uploadForm = document.createElement('form');
        var fileInput = uploadForm.appendChild(document.createElement('input'));
    
        fileInput.type = 'file';
        fileInput.name = 'images';
        fileInput.multiple = true;
    
        fileInput.click();
    });
    

    No need to add the uploadForm to the DOM.

提交回复
热议问题