微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – css隐藏“选择文件”按钮,但选择后显示文件

我想为图片上传制作自定义按钮.我可以通过下面的演示得到这个结果:

https://jsfiddle.net/algometrix/fgrbyo4z/

但我如何显示之后选择的文件名?或者甚至可能是图像的缩略图?就像我从弹出窗口的窗口中选择一个文件后,我希望它在我选择后在页面显示文件名”.

Javascript – 如果有人可以在这方面提供帮助,jQuery完全是一个选项.

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,#3498db);
  background-image: -ms-linear-gradient(top,#3498db);
  background-image: -o-linear-gradient(top,#3498db);
  background-image: linear-gradient(to bottom,#3498db);
  text-decoration: none;
}

解决方法

通过添加javascript,我们可以在输入上查看更改事件,并将名称放在页面上.请注意这些次要的HTML更改:
<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);
    }
  });
});

Working Fiddle

原文地址:https://www.jb51.cc/js/158736.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐