BootstrapDialog.Confirm在提交之前不等待

时间:2015-10-05 17:14:46

标签: javascript bootstrap-modal

我正在使用Bootstrap Dialog在删除邮件之前向用户显示一个样式很好的确认框。因此,我有类似

的东西
<form onsubmit='return confirmDelMsg()'>
...
</form>

javascript函数:

function confirmDelMsg() {
    BootstrapDialog.confirm('Are you sure you want to delete this message?', function(result) {
         return result;
     });
}

事情就是这个确认弹出窗口出现了一毫秒但不等我点击是或否。然后无论如何都要删除该消息。

那么我该如何解决这个问题?

这是我正在使用的库:Btw,它被称为库或框架,这个BootstrapDialog?

https://nakupanda.github.io/bootstrap3-dialog/

你知道其他任何图书馆吗?我也知道bootbox.js,但它有同样的错误......

编辑:

function confirmDeleteMsg() {
    return BootstrapDialog.confirm("Are you sure you want to delete this message?", function(result) {
       if (result) {
           var deleteMsgForm =  document.getElementById('deleteMsgForm');
           if (typeof(deleteMsgForm) != 'undefined' && deleteMsgForm != null) {
              alert("HERE"); // this here is executing!!!
           }
           deleteMsgForm.submit(); // this is not executing?! why?
        }
    });
}

1 个答案:

答案 0 :(得分:0)

可能是因为confirmDelContact在确认之前返回。试试:

<form onsubmit='return false;'>
...
</form>

然后在这里使用Javascript或jQuery提交:

function confirmDelMsg() {
    BootstrapDialog.confirm('Are you sure you want to delete this message?', function(result) {
         if (result)
             document.getElementById("deleteMsgForm").submit();
     });
}

注意这一点:

请注意,submit()函数提交时没有任何name属性。因此,要小心,因为很多人这样做:

<form id="deleteMsgForm" action="" method="post" onsubmit="return false;">
    <button type="submit" name="deleted" onclick="confirmDelMsg();">Delete Message</button>
    <input type="hidden" name="msgId" value="x" />
</form>

<?php
if (isset($_POST["deleted"])) {
   // this will NOT work with javascript submit() function!!!
   // because the submit() now doesn't have a name attr
}
// use instead the following:
if (isset($_POST["msgId"])) {
   deleteMsg($_POST["msgId"]);
}
?>
相关问题