How to trigger a <input type=“file”> element

2019-07-13 13:25发布

问题:

Is there a way to trigger the click even of :

<input id="test" type="file">

?

回答1:

I was looking for the same thing and found an answer at trigger file upload dialog using javascript/jquery and a running example is at http://jsfiddle.net/CSvjw/

it simply does

$('#test').trigger('click');


回答2:

Dont use display:none or visibility:hidden initially in the css

In Jquery:

$(document).ready(function() {
 $('#test').hide(); 
});


回答3:

There is no way to trigger the upload window programmatically, however if you just want to hide the ugly "choose a file"-button and inputfield, there is a simple solution.

Just hide the input using any old technique and add a label refering to it. You can then style the label any way you want. Clicking the label will trigger the upload window.

For example:

<input id="test" type="file" style="display: none" />
<label for="test" class="make-it-look-like-a-button">Upload</label>



回答4:

As far as I know, there is no way to trigger it programmatically for security reasons. You got to click it explicitly.



回答5:

Not possible (security reasons).



回答6:

No, there isn't, and this is a good thing as it would be extremely annoying for the user. Whether a user wants to upload some file is in is control only, personally I would hate a site automatically sticking an Open Dialog into my nose when I visit it.



标签: jquery input