Quiero mostrar un mensaje de confirmación de Sweet Alert 2 al usuario después de enviar el formulario como este:
<form id="myForm" data-flag="0" action="" method="POST"> @csrf <input type="checkbox" id="btn-submit" name="wallet_checked" onchange="this.form.submit();"> </form> Como puede ver, he usado onchange="this.form.submit();" para enviar el formulario sin usar el botón Enviar y funciona bien.
Luego intenté agregar esto como secuencia de comandos para mostrar el mensaje SweetAlert al usuario:
$(document).on('click', '#btn-submit', function(e) { e.preventDefault(); let form = $(this).parents('form'); swal( { title: "Attention!", text: "Are you sure you want to make this", type: "warning", allowEscapeKey: false, allowOutsideClick: false, showCancelButton: true, confirmButtonColor: "#DD6B55", confirmButtonText: "Yes", cancelButtonText: "No", showLoaderOnConfirm: true, closeOnConfirm: false }, function (isConfirm) { if (isConfirm) { console.log("YES"); } return false; }); }); Entonces, la función muestra el mensaje Sweet Alert en la pantalla después de hacer clic en la casilla de verificación, pero cuando presiono el botón Sí , no se ejecuta console.log("YES"); .
Y esto significa que if (isConfirm) { at function (isConfirm) { no funciona correctamente.
Entonces, ¿cómo resolver este problema? ¿Qué tiene de malo isConfirm ?
Cambia esto a:
$(document).on('click', '#btn-submit', function(e) { e.preventDefault(); let form = $(this).parents('form'); swal( { title: "Attention!", text: "Are you sure you want to make this", type: "warning", allowEscapeKey: false, allowOutsideClick: false, showCancelButton: true, confirmButtonColor: "#DD6B55", confirmButtonText: "Yes", cancelButtonText: "No", showLoaderOnConfirm: true, closeOnConfirm: false }) .then((isConfirm) => { if (isConfirm) { console.log("YES"); } return false; }); });