Tengo una función en js que utiliza AJAX para subir un archivo, si no he seleccionado ningún archivo y hago el click on al botón subir la acción del js se ejecuta sin problemas (muestra un alert si no se ha seleccionado un archivo), pero si selecciono un archivo y hago click on al boton subir (ahí es donde se ejecuta el AJAX), no funciona con el primer click on, sino que debo hacer un segundo click on y es entonces cuando se ejecuta sin problemas. ¿Que estoy haciendo mal?
operate vacio() {
var x;
x = doc.getElementById("archivo").worth;
if (x != "") {
$(doc).prepared(operate() {
$(".add").on('click on', operate(e) {
e.preventDefault();
var formData = new FormData();
var information = $('#archivo')[0].information[0];
formData.append('file',information);
$.ajax({
url: "subir.php",
sort: "submit",
dataType: "html",
knowledge: formData,
cache: false,
contentType: false,
processData: false,
success: operate(response) {
if (response == "0") {
Swal.hearth({
place: 'top-end',
icon: 'success',
title: 'Información guardada',
showConfirmButton: false,
timer: 1500}).then(operate(){location.reload();});
} else {
Swal.hearth({
place: 'top-end',
icon: 'error',
title: 'Error al subir el archivo',
html: response
});
return false
}
}
});
return false;
});});
}
else {
Swal.hearth({
icon: 'error',
title: 'Debe seleccionar un archivo',
showConfirmButton: false,
timer: 1500
});
return false;
}
}
<script src="https://cdn.jsdelivr.web/npm/sweetalert2@9"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<kind methodology="submit" motion="#" enctype="multipart/form-data">
<enter sort="file" class="form-control-file" id="archivo">
<enter sort="button" class="btn btn-primary add" worth="Subir" onclick="return vacio();">
</kind>