bootbox.confirm()无法正常工作

问题描述 投票:0回答:3

bootbox.confirm()有什么问题?这是我的代码:

var record = bootbox.confirm("Are you sure you want to delete", function (res) {
    if (res == true) {
        return true;
    } else {
        return false;
    }
}
});

它不等待确认并默认返回true。如果是在最后使用return false来阻止它消失

var record = bootbox.confirm("Are you sure you want to delete", 
    function (res) {
        if(res == true)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
});
return false;

然后它没有显示任何针对点击“确定”或“取消”的行动

如果我在真节中使用任何简单警报来检查它在真实部分中输入的行为并显示简单警报并显示所需的点击按钮值,但如果在同一部分中使用return true但它不起作用

var record = bootbox.confirm("Are you sure you want to delete", function (res) {
    if (res == true)
        alert('true');
    else
        alert('false')
}
});
return false;
javascript jquery twitter-bootstrap twitter-bootstrap-3 bootbox
3个回答
2
投票

我得到了解决方案,bootstrap.confirm()在行为上是异步的,所以我们不能停止它的流程,但我们可以改变我们的逻辑来使用它像

首先,我们必须停止其默认行为,然后将其用作我们的要求

无论我们想做什么,都把它放在代码的真实部分下面

         e.preventDefault();// used to stop its default behaviour 
             bootbox.confirm(Are you sure you want to delete ? , function (confirmed) {
                 if (confirmed == true) {
                  // place here what we want to do
                     __doPostBack('btnDelete', 'OnClick');
                 } 
                  else 
                  {

                   }
             });

0
投票

您的代码中有一个额外的花括号只需删除它。

 var record = bootbox.confirm("Are you sure you want to delete", function (res) {
     if (res == true) {
         return true;
     } else {
         return false;
     }
     //}   // invalid closing brace
 });

这可能是您的确认框不起作用的问题之一。

快乐编码:)


0
投票

可能更容易理解......我们分开了客户端和服务器端事件。确认后,我们会触发服务器端点击事件...以防您不喜欢

__doPostBack('btnDelete', 'OnClick')

服务器端被省略...

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<style>
    .hidden{
        display:none;
    }
</style>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="Content/bootstrap.min.css" rel="stylesheet" />
    <script src="Scripts/jquery-3.0.0.min.js"></script>
    <script src="Scripts/bootstrap.min.js"></script>
    <script src="Scripts/bootbox.min.js"></script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="btnTest" runat="server" Text="Confirm 1" OnClientClick="return confirmClick(this);" />
            <asp:Button ID="btnReal" runat="server" Text="Actual 1" CssClass="real-button hidden"  OnClick="btnTest_Click" />
        </div>

        <div>
            <asp:Button ID="btnTest2" runat="server" Text="Confirm 2" OnClientClick="return confirmClick(this);" />
            <asp:Button ID="btnReal2" runat="server" Text="Actual 2" CssClass="real-button hidden"  OnClick="btnTest_Click" />
        </div>

        <div>
            <asp:Button ID="btnTest3" runat="server" Text="Confirm 3" OnClientClick="return confirmClick(this);" />
            <asp:Button ID="btnReal3" runat="server" Text="Actual 3" CssClass="real-button hidden"  OnClick="btnTest_Click" />
        </div>

    </form>
</body>
</html>

<script>
    function confirmClick(e) {

        var result = bootbox.confirm({
            message: "This is a confirm with custom button text and color! Do you like it?",
            buttons: {
                confirm: {
                    label: 'Yes',
                    className: 'btn-success'
                },
                cancel: {
                    label: 'No',
                    className: 'btn-danger'
                }
            },
            callback: function (result) {
                var real = $(e).parent().find('.real-button');
                if (result && real)
                {
                    console.log('This was logged in the callback: ' + result + real.val());
                    real.click();
                }
            }
        });

        return true;
    }
</script>
© www.soinside.com 2019 - 2024. All rights reserved.