How to remove dynamically selected images before submitting

0

I would like to know how to remove selected images from the input file before submitting them, I am new to JavaScript, I have seen several examples, however without the implementation with the submit button and I do not know how get these values, it would look something like this:

Somethingbasedon post I saw here in the community

    
asked by anonymous 13.06.2016 / 17:26

1 answer

1

Hello, look at this example, it might 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><trdata-row><td><inputtype="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>
    
13.06.2016 / 20:14