JavaScript sweetAlert popup closes itself after a quick second

妖精的绣舞 提交于 2019-12-08 05:01:00

问题


I have a SweetAlert popup, but it is closing itself automatically. Normally it should stay until user clicks 'OK'. (I have included and tested all SweetAlert files.)

<button class="btn btn-success" type="submit" name="submit" onclick="myFunction()">Submit</button>

<script>
function myFunction() {
    swal("Submitted!", "Your Issue has been submitted!", "success");
}
</script>

回答1:


Looks like it's a form submit, So when you click the button the page refreshes.That's why you see the alert for a second and it hides.

<button class="btn btn-success" type="submit" name="submit" onclick="myFunction()">Submit</button>

You must change the button type to be button.

   <button class="btn btn-success" type="button" onclick="myFunction()">Submit</button>



回答2:


Note that if your button is part of a form submit, then issue 284 suggests it's not currently supported. You can change your button to a regular button (rather than a submit button) and programmatically submit the form from the callback in a sweetalert callback.




回答3:


I have a similar problem.. when click outside of the popup, the popup window closes automatically and change the page very fast...

I tried this code for stop close sweetalert itself, and refresh the page when the user click "OK" of sweetalert window.

HTML:

<form  method="post" action="contact.php" name="myform" onsubmit="return myFunction()">

<input type="text" class="form-control" name="full" placeholder="* Full Name" required="" >

<input type="text" class="form-control" name="last" placeholder="* Last Name" required="">

<input type="submit" value="send" name="sends" class="btn btn-lg">

</form>

javascript

<script type="text/javascript">
function myFunction() {
  var full=document.myform.full.value;
  var last=document.myform.last.value;
  if (full==null || last==null){
    swal("Error...!!!!!!");
    return false;
}else{
    swal("Congrats!", ", Your account is created!", "success");
    return true;}
}
</script>


来源:https://stackoverflow.com/questions/29348785/javascript-sweetalert-popup-closes-itself-after-a-quick-second

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!