Quiero borrar la imagen cuyo formato de imagen no es válido. Lo he intentado varias veces pero no funcionó. Cómo puedo lograrlo. Usando jQuery 3.6.0
$(':file').change(function(){ var fileArray = this.files; var validImageTypes = ["image/gif", "image/jpeg", "image/png"]; $.each(fileArray,function(i,v){ type = v.type; if ($.inArray(type, validImageTypes) < 0) { alert('Only jpg, jpeg and png is allowed.'); // here i want to clear the image which is selected }; }) }); Intenté usar $(this).val("") pero no funcionó. Por favor ayuda.
this apunta la función llamada en cada uno.
Utilice una función de flecha para enlazar automáticamente con el contexto principal (el elemento de archivo).
$(':file').change(function() { var fileArray = this.files; var validImageTypes = ["image/gif", "image/jpeg", "image/png"]; $.each(fileArray, (i, v) => { type = v.type; if ($.inArray(type, validImageTypes) < 0) { alert('Only jpg, jpeg and png is allowed.'); // here i want to clear the image which is selected $(this).val(""); }; })});