Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

javascript - issue with multiple custom choose file

this is my code i want more than 1 custom choose file upload in place of this label

<form>
<input type="file" id="file-upload" multiple required />
<image for="file-upload">Upload file</>
<div id="file-upload-filename"></div>
input[type="file"] { z-index: -1; position: absolute; opacity: 0; }
   input:focus + label {
  outline: 2px solid;
   }
  <script>
  var input = document.getElementById( 'file-upload' );
  var infoArea = document.getElementById( 'file-upload-filename' );

  input.addEventListener( 'change', showFileName );

  function showFileName( event ) {


   var input = event.srcElement;

   var fileName = input.files[0].name;

   infoArea.textContent = 'File name: ' + fileName;
          }
question from:https://stackoverflow.com/questions/66062032/issue-with-multiple-custom-choose-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There's actually no problem of HTML or JS, u have a -1 z-index, that's because the input wasn't showing up, just change the z-index value to 1

Also, you only take one file, make a for to take all files

var input = document.getElementById('file-upload');
var infoArea = document.getElementById('file-upload-filename');

input.addEventListener('change', showFileName);

function showFileName(event) {
  var input = event.srcElement;
  infoArea.innerHTML = "";
  for (let index = 0; index<input.files.length; index++) {
    const element = input.files[index]
    var fileName = element.name;
    infoArea.innerHTML += '<p>File name: ' + fileName+"</p>";
  }
}
input[type="file"] { 
   z-index: 1;
   position: absolute;
   opacity: 0;
}

input:focus + label {
  outline: 2px solid;
}
<form>
  <input type="file" id="file-upload" multiple required />
  <label for="file-upload">Upload file</label>
</form>
<div id="file-upload-filename"></div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...