jQuery trigger file input

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

    Try this, it's a hack. the Position:absolute is for Chrome and trigger('change') is for IE.

    var hiddenFile = $("");
    $('body').append(hiddenFile);
    
    $('#aPhotoUpload').click(function () {
        hiddenFile.trigger('click');
        if ($.browser.msie)
            hiddenFile.trigger('change');
    });
    
    hiddenFile.change(function (e) {
        alert('TODO');
    });
    

提交回复
热议问题