Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

222
Views
vuelva a cargar el archivo type=una vez cargado hasta enviarlo

Estoy creando un formulario donde necesito cargar imágenes donde puedo cambiar la imagen cargada hasta que envíe el formulario

 function previewImage() { var thisElement = event.target var file = thisElement.files; var fileReader = new FileReader(); fileReader.onload = function(event) { thisElement.nextElementSibling.setAttribute("src", event.target.result); } thisElement.style.display = 'none'; fileReader.readAsDataURL(file[0]) };
 <input type="file" id="file1" accept="image/*" onchange="previewImage();"> <img id="display1"><br> <input type="file" id="file2" accept="image/*" onchange="previewImage();"> <img id="display2"><br> <input type="file" id="file3" accept="image/*" onchange="previewImage();"> <img id="display3"><br> <input type="file" id="file4" accept="image/*" onchange="previewImage();"> <img id="display4"><br>

Quiero cambiar el archivo cargado, pero no puedo hacerlo una vez que lo cargué.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Simplemente elimine esta línea que oculta la entrada de su archivo una vez que se haya seleccionado un archivo:

 thisElement.style.display = 'none';
about 4 years ago · Juan Pablo Isaza Report

0

Yo haría esto por:

  • Envuelva cada imagen en una <label> para la entrada relevante
  • Actualice su evento de imagen cargada para apuntar a la imagen específicamente usando nextElementSibling.querySelector("img")
  • Envuelva fileReader.readAsDataURL(file[0]) en un if para evitar un error cuando el usuario cancela la selección de archivos.

De esa manera, puede hacer clic en la imagen para cambiarla después de la primera vez que se configura.

 function previewImage() { var thisElement = event.target var file = thisElement.files; var fileReader = new FileReader(); fileReader.onload = function(event) { thisElement.nextElementSibling.querySelector("img").setAttribute("src", event.target.result); } thisElement.style.display = 'none'; if (file.length) { fileReader.readAsDataURL(file[0]) } };
 <input type="file" id="file1" accept="image/*" onchange="previewImage();"> <label for="file1"><img id="display1"></label><br> <input type="file" id="file2" accept="image/*" onchange="previewImage();"> <label for="file2"><img id="display2"></label><br> <input type="file" id="file3" accept="image/*" onchange="previewImage();"> <label for="file3"><img id="display3"></label><br> <input type="file" id="file4" accept="image/*" onchange="previewImage();"> <label for="file4"><img id="display4"></label><br>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!