简体   繁体   English

自定义文件上传按钮以使用JavaScript显示文件名

[英]Custom File Upload button to show file name using JavaScript

I'm new to JavaScript and I'm having trouble changing the label of my customized File Upload button to show the file name after file has been selected. 我是JavaScript的新手,在选择文件后,无法更改自定义的“文件上载”按钮的标签以显示文件名。

Tried to follow these steps . 试图遵循以下步骤

The button shows correctly and the file is uploaded, but I can't manage to make the JS work to show the uploaded files name on the button. 该按钮正确显示并且文件已上传,但是我无法使JS工作以在按钮上显示已上传文件的名称。

 var inputs = document.querySelectorAll('.wpcf7-file'); Array.prototype.forEach.call(inputs, function(input) { input.addEventListener('change', function(e) { var label = document.querySelectorAll('.custom-file-button'); var fileName = ''; fileName = e.target.value.split('\\\\').pop(); if (fileName) { label.innerHTML = fileName; } else label.innerHTML = labelVal; }); }); 
 label.custom-file-button { position: relative; } label.custom-file-button:before { content: "📷 Take or upload picture"; position: absolute; left: 0; padding: 10px; background: #1b87d5; color: #fff; width: auto; text-align: center; border-radius: 5px; cursor: pointer; } label.custom-file-button:hover:before { background: #146bac; } .custom-file-input { visibility: hidden; } 
 <div class="limit-to-view"> <label class="wpcf7-form-control-wrap custom-file-button"> <span class="wpcf7-form-control-wrap custom-file-input"> <input type="file" name="custom-file-input" size="40" class="wpcf7-form-control wpcf7-file" accept=".jpg,.jpeg,.png,.gif,.pdf,.doc,.docx,.ppt,.pptx,.odt,.avi,.ogg,.m4a,.mov,.mp3,.mp4,.mpg,.wav,.wmv" aria-invalid="false"> </span> </label> </div> 

.querySelectorAll() returns a NodeList , not a single element. .querySelectorAll()返回NodeList ,而不是单个元素。 Substituted .closest() for .querySelectorAll() to select closest ancestor <label> element to the current <input type="file"> element. .closest()替换为.querySelectorAll()以选择与当前<input type="file">元素最接近的祖先<label> <input type="file">元素。 Since the <input type="file"> element is the child of the <label> setting .innerHTML to the file name would overwrite the <input type="file"> element. 由于<input type="file">元素是<label>设置的子元素,因此将.innerHTML设置为文件名将覆盖<input type="file">元素。 The CSS does not display a <span> element that is first child of the <label> , added the <span> to display file name as .nextElementSibling of <label> element. CSS不会显示<label>第一个子元素<span>元素,而是添加了<span>以将文件名显示为<label>元素的.nextElementSibling You can use e.target.files[0].name to get the file name. 您可以使用e.target.files[0].name来获取文件名。

 var inputs = document.querySelectorAll('.wpcf7-file'); Array.prototype.forEach.call(inputs, function(input) { input.addEventListener('change', function(e) { var label = e.target.closest('.custom-file-button'); label.nextElementSibling.innerHTML = e.target.files[0].name; }); }); 
 label.custom-file-button { position: relative; } label.custom-file-button:before { content: "📷 Take or upload picture"; position: absolute; left: 0; padding: 10px; background: #1b87d5; color: #fff; width: auto; text-align: center; border-radius: 5px; cursor: pointer; } label.custom-file-button:hover:before { background: #146bac; } .custom-file-input { visibility: hidden; } 
 <div class="limit-to-view"> <label class="wpcf7-form-control-wrap custom-file-button"> <span class="wpcf7-form-control-wrap custom-file-input"> <input type="file" name="custom-file-input" size="40" class="wpcf7-form-control wpcf7-file" accept=".jpg,.jpeg,.png,.gif,.pdf,.doc,.docx,.ppt,.pptx,.odt,.avi,.ogg,.m4a,.mov,.mp3,.mp4,.mpg,.wav,.wmv" aria-invalid="false"> </span> </label> <span></span> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM