jQuery trigger file input

后端 未结 21 1896
梦如初夏
梦如初夏 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:03

    I think i understand your problem, because i have a similar. So the tag have the atribute for, you can use this atribute to link your input with type="file". But if you don't want to activate this using this label because some rule of your layout, you can do like this.

    $(document).ready(function(){
      var reference = $(document).find("#main");
      reference.find(".js-btn-upload").attr({
         formenctype: 'multipart/form-data'
      });
      
      reference.find(".js-btn-upload").click(function(){
        reference.find("label").trigger("click");
      });
      
    });
    .hide{
      overflow: hidden;
      visibility: hidden;
      /*Style for hide the elements, don't put the element "out" of the screen*/
    }
    
    .btn{
      /*button style*/
    }
    
    

    Of course you will adapt this for your own purpose and layout, but that's the more easily way i know to make it work!!

提交回复
热议问题