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

2019-07-13 13:32发布

Is there a way to trigger the click even of :

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

?

标签: jquery input
6条回答
不美不萌又怎样
2楼-- · 2019-07-13 13:46

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.

查看更多
霸刀☆藐视天下
3楼-- · 2019-07-13 13:56

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

In Jquery:

$(document).ready(function() {
 $('#test').hide(); 
});
查看更多
兄弟一词,经得起流年.
4楼-- · 2019-07-13 13:57

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>

查看更多
孤傲高冷的网名
5楼-- · 2019-07-13 14:00

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

查看更多
beautiful°
6楼-- · 2019-07-13 14:01

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');
查看更多
7楼-- · 2019-07-13 14:06

Not possible (security reasons).

查看更多
登录 后发表回答