4

在一个 aspx 页面中,有一个像这样的 asp:linkbutton:

<asp:LinkButton runat="server" ID="btExit" Text="Exit"
                OnClientClick="javascript:return confirmExit();" 
                EnableViewState="false" 
                OnClick="ExitButtonClick"></asp:LinkButton>

这是javascript函数:

<script type="text/javascript">
function confirmExit() {
    bootbox.confirm("Are you sure?", function (confirmed) {
        return confirmed;
    });
}
</script>

问题是,据我所知,bootbox.confirm 是异步工作的,后面代码上的 ExitButtonClick 函数无需等待用户确认即可执行。

我找到了一个有效的解决方案,使用隐藏按钮:

<asp:LinkButton runat="server" ID="btExit" Text="Exit"></asp:LinkButton>
<asp:Button runat="server" ID="btExitHidden" onclick="ExitButtonClick" style="display:none;" />

这是javascript部分:

<script type="text/javascript">
    $("#btExit").click(function (e) {
        e.preventDefault();
        bootbox.confirm("Are you sure?", function (confirmed) {
            if (confirmed) {
                $("#btExitHidden").click();
            }
        });
    });
</script>

我的问题是,是否有更“漂亮”和“标准”的方式与 Bootbox.confirm 同步工作,而不使用隐藏按钮。

4

2 回答 2

0

您可以通过这种方式制作自定义同步引导盒功能:

function ayncBootbox(message, cb = null) { // cb : function
        return new Promise(resolve => {
            bootbox.confirm({
                message: message,
                buttons: {
                    confirm: {
                        label: "Yes"
                    },
                    cancel: {
                        label: "No"
                    }
                },
                callback: cb ? result => cb(resolve, result) : result => resolve(result)
            })
        })
}

那么如果你需要做一些额外的事情,你可以通过传递一个自定义回调来调用它

var result = await ayncBootbox("message", (resolve, result) => resolve(result))

要不就

var result = await ayncBootbox("message")

PS:不要忘记将调用者函数也设为异步 :) 如果需要,您可以使用拒绝更多地扩展此代码

于 2021-07-15T13:08:16.343 回答
-2

My solution

@Html.ActionLink("Delete", "DeleteReport", new { id = item.Id }, new { @class = "btn btn-danger", onclick = String.Format("return ASE.ConfirmAction(this.href, 'Delete {0}?');", item.Name) })
var ASE = {
    ConfirmAction: function (href, text) {
        bootbox.confirm(text, function (result) {
            if (result)
                window.location = href;
        });

        return false;
    }
}
于 2014-08-28T09:44:04.240 回答