это сработало для меня:
JS:
$('#fileinput').trigger('click');
HTML:
<div class="hiddenfile">
<input name="upload" type="file" id="fileinput"/>
</div>
CSS:
.hiddenfile {
width: 0px;
height: 0px;
overflow: hidden;
}
>>> Еще один, который работает в кросс-браузерном режиме: <<< </strong>
Идея состоит в том, что вы накладываете невидимую огромную кнопку «Обзор» поверх своей пользовательской кнопки.
Поэтому, когда пользователь нажимает вашу пользовательскую кнопку, он фактически нажимает кнопку «Обзор» собственного поля ввода.
JS Fiddle: http://jsfiddle.net/5Rh7b/
HTML:
<div id="mybutton">
<input type="file" id="myfile" name="upload"/>
Click Me!
</div>
CSS:
div#mybutton {
/* IMPORTANT STUFF */
overflow: hidden;
position: relative;
/* SOME STYLING */
width: 50px;
height: 28px;
border: 1px solid green;
font-weight: bold
background: red;
}
div#mybutton:hover {
background: green;
}
input#myfile {
height: 30px;
cursor: pointer;
position: absolute;
top: 0px;
right: 0px;
font-size: 100px;
z-index: 2;
opacity: 0.0; /* Standard: FF gt 1.5, Opera, Safari */
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; /* FF lt 1.5, Netscape */
}
JavaScript:
$(document).ready(function() {
$('#myfile').change(function(evt) {
alert($(this).val());
});
});