How to remove dynamically selected images before Submit

Asked

Viewed 617 times

0

I wonder how I can remove selected images from input file before sending them, I’m new in Javascript, I saw several examples, but without the implementation with the "Submit" button and I don’t know how to get these values, it would be something like this:

inserir a descrição da imagem aqui

Something based on a post I’ve seen here in the community

  • You can show the code you are using and refer to any libraries you use (jQuery, Boostrap, Mootools, etc...)

1 answer

1

Hello, take this example, maybe it will help you:

$(document).on("click", "[data-remove-file]", function (e) {

  e.preventDefault();
  
  $(e.target).closest("[data-row]").remove();

});

$(document).on("submit", "form", function (e) {

  //e.preventDefault();
  
  $(e.target).find("[data-mark-remove-file]").each(function (i, chk) {
    
    if ($(chk).is(":checked"))
      $(chk).closest("[data-row]").remove(); 
  });
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<form>
<table>
  <tr data-row>
    <td><input type="file"></td>
    <td><input type="checkbox" data-mark-remove-file>remove before submit</td>
    <td><a href="#" data-remove-file>remove now</a></td>
  </tr>
  <tr data-row>
    <td><input type="file"></td>
    <td><input type="checkbox" data-mark-remove-file>remove before submit</td>
    <td><a href="#" data-remove-file>remove now</a></td>
  </tr>
  <tr data-row>
    <td><input type="file"></td>
    <td><input type="checkbox" data-mark-remove-file>remove before submit</td>
    <td><a href="#" data-remove-file>remove now</a></td>
  </tr>
</table>

<button>submit</button>

</form>

  • interesting way to answer the question, but he wants to use a Multiple input, and remove only one file from that input, and not several single inputs... I’m in the same trouble he is.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.