css hide“选择文件”按钮,但选择后显示文件 [英] css hide "Choose File" button but display file after select
问题描述
我想为图片上传制作自定义按钮。我可以通过下面的演示得到这个结果:
I would like to make custom button for an image upload. I am able to get this result with the demo below:
https://jsfiddle.net/algometrix/fgrbyo4z/
但我如何显示之后选择的文件名?或者甚至可能是图像的缩略图?就像我从弹出窗口的窗口中选择一个文件后,我希望它在我选择后在页面上显示文件名。
But HOW CAN I DISPLAY the file name selected after? Or maybe even a thumbnail of an image if possible? Like after I choose a file from the window that pops open I would like it to display 'the file name' on the page after I selected it.
Javascript - 如果有人可以在这方面提供帮助,jQuery完全是一个选项。
Javascript - jQuery is totally an option here if anyone can help in that area.
HTML
<div>
<div style="display:block;text-align:center;margin-top:20%;">
<label for="files"> <span class="btn">Select Image</span></label>
<input style="visibility: hidden; position: absolute;" id="files" class="form-control" type="file" name="files">
</div>
</div>
CSS
.btn {
font-family: Arial;
color: #ffffff;
font-size: 20px;
background: #3f88b8;
padding: 10px 20px 10px 20px;
text-decoration: none;
}
.btn:hover {
background: #3cb0fd;
background-image: -webkit-linear-gradient(top, #3cb0fd, #3498db);
background-image: -moz-linear-gradient(top, #3cb0fd, #3498db);
background-image: -ms-linear-gradient(top, #3cb0fd, #3498db);
background-image: -o-linear-gradient(top, #3cb0fd, #3498db);
background-image: linear-gradient(to bottom, #3cb0fd, #3498db);
text-decoration: none;
}
推荐答案
添加了javascript,我们可以在输入上查看更改
事件,并将名称放在页面上。请注意这些次要的HTML更改:
With the addition of javascript, we can just watch for the change
event on the input, and put the name on the page. Please note these minor HTML changes:
<div class="file-upload">
<label for="upload-1" class="btn">Upload</label>
<input type="file" id="upload-1">
<p class="file-name">Please select a file.</p>
</div>
使用此jQuery:
jQuery(function($) {
$('input[type="file"]').change(function() {
if ($(this).val()) {
var filename = $(this).val();
$(this).closest('.file-upload').find('.file-name').html(filename);
}
});
});
这篇关于css hide“选择文件”按钮,但选择后显示文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!