[英]jquery bootstrap modal closing after swal alert
I have this code here我这里有这个代码
I am trying to close bootstrap
modal after swal
is clicked ok:单击
swal
后,我试图关闭bootstrap
模式:
swal({
title:'',
text:'Thanks, we will contact you !',
type: 'success'
});
$("#modal").on('hidden.bs.modal', function () {
alert('boo');
$(this).data('bs.modal', null);
}).trigger();
Nothing is happening, not even alert
, only if I click the close button on the modal, it shows an alert
and closes.什么都没有发生,甚至没有
alert
,只有当我点击模态上的关闭按钮时,它才会显示alert
并关闭。
You should use the .then()
method which accepts a callback function, in that callback you'd close the modal.您应该使用接受回调函数的
.then()
方法,在该回调中您将关闭模态。
Here a demo to demonstrate how this'll work:这里有一个演示来演示这将如何工作:
I don't know your markup, so, in my example, I have a button to open a modal and when it's clicked a
Sweet Alert
popup shows containing two buttons, if the OK button of that popup is pressed, the modal will immediately be hidden.我不知道你的标记,所以,在我的例子中,我有一个按钮来打开一个模态,当它被点击时,一个
Sweet Alert
弹出窗口显示包含两个按钮,如果按下那个弹出窗口的OK按钮,模态将立即隐。The modal's structure is taken from the
BootStrap
's documentation.模态的结构取自
BootStrap
的文档。
var myModal = $('#exampleModal'); myModal.on('show.bs.modal', function() { swal({ title: 'Close modal ?', text: 'Do you want to close the modal ?', icon: 'warning', buttons: true }).then(function(value) { if(value === true) { myModal.modal('hide'); } }); });
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.2/css/bootstrap.min.css" /> <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal"> Launch demo modal </button> <!-- Modal --> <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="exampleModalLabel">Modal title</h5> </div> <div class="modal-body"> <p class="alert alert-success">There are no any buttons to close the modal, it's closed either by pressing the backdrop or by pressing <strong>OK</strong> button of the Sweet Alert's popup.</p> </div> <div class="modal-footer"> normally, some buttons are placed here... </div> </div> </div> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.0/sweetalert.min.js"></script>
Hope I pushed you further.希望我能进一步推动你。
The answers I found didn't work, because the hide.bs.modal
even is triggered before the promise from SWAL is returned.我发现这些问题的答案没有工作,因为
hide.bs.modal
返回从SWAL的承诺之前,甚至被触发。 This is my working function:这是我的工作功能:
$('#myModal').on("hide.bs.modal", function (e) {
if (!$('#myModal').hasClass('programmatic')) {
e.preventDefault();
swal.fire({
title: 'Are you sure?',
text: "Please confirm that you want to cancel",
type: 'warning',
showCancelButton: true,
confirmButtonText: 'Yes, cancel',
cancelButtonText: 'No, I clicked by mistake',
}).then(function(result) {
if (result.value) {
$('#myModal').addClass('programmatic');
$('#myModal').modal('hide');
e.stopPropagation();
} else {
e.stopPropagation();
}
});
}
return true;
});
$('#myModal').on('hidden.bs.modal', function () {
$('#myModal').removeClass('programmatic');
});
As you can see, I add a class to the modal whenever the close event is triggered by the code, so I use e.preventDefault()
to prevent the modal from closing.如您所见,每当代码触发关闭事件时,我都会向模态添加一个类,因此我使用
e.preventDefault()
来防止模态关闭。 Whenever the modal has been successfully hidden, I remove that class again, so it will work again the next time the user tries to close it.每当模式成功隐藏时,我都会再次删除该类,以便下次用户尝试关闭它时它会再次工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.