jQuery trigger file input

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

    i had problems with custom client side validation for while using a fake button to trigger it and @Guillaume Bodi's solution worked for me (also with opacity: 0; on chrome)

    $("#MyForm").on("click", "#fake-button", function () {
            $("#uploadInput").focus().trigger("click");
        });
    

    and css style for upload input

    #uploadInput {
    opacity: 0.0; 
    filter: alpha(opacity=0); /* IE lt 8 */
    -ms-filter: "alpha(opacity=0)"; /* IE 8 */
    -khtml-opacity: 0.0; /* Safari 1.x */
    -moz-opacity: 0.0;
    }
    

提交回复
热议问题