如何从JavaScript中的AJAX函数创建异步确认框?

发布于 2025-01-19 04:31:18 字数 2427 浏览 0 评论 0原文

我正在尝试创建一个自定义确认框,该框将从 ajax 函数异步运行,该函数将调用后面 C# 代码中隐藏的删除按钮,但我似乎无法让它正确等待。我只是希望它弹出一个确认框,然后等待处理其他任何内容,直到用户选择“是”或“否”。我正在使用 Promise 来等待。然而,它似乎并没有做任何事情。如果用户单击了“是”,但代码没有进入该块,那么隐藏的“删除”按钮单击应该是最后发生的事情。我确信我缺少一些非常简单的东西。以下是我正在使用的代码:

<div id="confirm" class="confirm">
    <div class="message">The project name already exists!  Do you wish to delete the project?</div>
    <br />
    <div>
        <button id="true_btn" class="true_btn">Yes</button>
        <button id="false_btn" class="false_btn">No</button>
    </div>
</div>

function checkDirectory(projectName) {
        var onError = false;
        $.ajax({
            type: 'GET',
            url: "<%= ConfigurationManager.AppSettings["checkDirUrl"] %>" + "/" + projectName,
            //async: false,
            success: async function (result) {
                if (result === "folder exists")
                {
                    //if (confirm('The project name already exists!  Do you wish to delete the project?')) {
                    if (await deleteDemo() === 1) {
                        alert('After deleteDemo');
                        $("#<%= btnHiddenDelete.ClientID %>").click();
                    } else {
                        onError = true;
                        $("#error").text("Project name already exists!");
                    }
                } else {
                    window.location = ("/Database.aspx");
                }
            },
            error: function (result) {
                onError = true;
                $("#error").text("Error accessing project folder!");
            }
        });
        return onError;
    }

async function confirmationBox() {
        return new Promise(function(resolve, reject) {
            var confirmation_modal = $("#confirm");
            var true_el = $("#true_btn");
            var false_el = $("#false_btn");

            alert('In confirmationBox');

            confirmation_modal.show();

            true_el.click(function() {
                alert('yes');
                resolve(1);
            });

            false_el.click(function() {
                alert('no');
                resolve(0);
            });
        });
    }

    async function deleteDemo() {
        var result = await confirmationBox();
        alert(result);
        
        return result;
    }

I am trying to create a custom confirmation box that will run asynchronously from an ajax function that will call a hidden Delete button in the C# code behind, but I can't seem to get it to wait properly. I simply want it to bring up a confirmation box and then wait to process anything else until the user has chosen yes or no. I am using a Promise for waiting. However, it doesn't seem to be doing anything. The hidden Delete button click should be the last thing that happens if the user has clicked yes, but the code is not getting into that block. I am sure there is something very simple that I am missing. The following is the code that I am using:

<div id="confirm" class="confirm">
    <div class="message">The project name already exists!  Do you wish to delete the project?</div>
    <br />
    <div>
        <button id="true_btn" class="true_btn">Yes</button>
        <button id="false_btn" class="false_btn">No</button>
    </div>
</div>

function checkDirectory(projectName) {
        var onError = false;
        $.ajax({
            type: 'GET',
            url: "<%= ConfigurationManager.AppSettings["checkDirUrl"] %>" + "/" + projectName,
            //async: false,
            success: async function (result) {
                if (result === "folder exists")
                {
                    //if (confirm('The project name already exists!  Do you wish to delete the project?')) {
                    if (await deleteDemo() === 1) {
                        alert('After deleteDemo');
                        $("#<%= btnHiddenDelete.ClientID %>").click();
                    } else {
                        onError = true;
                        $("#error").text("Project name already exists!");
                    }
                } else {
                    window.location = ("/Database.aspx");
                }
            },
            error: function (result) {
                onError = true;
                $("#error").text("Error accessing project folder!");
            }
        });
        return onError;
    }

async function confirmationBox() {
        return new Promise(function(resolve, reject) {
            var confirmation_modal = $("#confirm");
            var true_el = $("#true_btn");
            var false_el = $("#false_btn");

            alert('In confirmationBox');

            confirmation_modal.show();

            true_el.click(function() {
                alert('yes');
                resolve(1);
            });

            false_el.click(function() {
                alert('no');
                resolve(0);
            });
        });
    }

    async function deleteDemo() {
        var result = await confirmationBox();
        alert(result);
        
        return result;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文