jQuery 中的简单模式

发布于 2024-07-11 11:24:52 字数 1307 浏览 3 评论 0 原文

我在 jQuery 中使用 SimpleModal,并且有一个确认对话框。 如果结果是Yes,我必须在该对话框中调用my.php。 不过,我已经完成了代码,并且仍在寻找想法。 我该怎么做?

$(document).ready(function () {
    $('#confirmDialog input.confirm, #confirmDialog a.confirm').click(function (e) {
        e.preventDefault();
        // Example of calling the confirm function.
        // You must use a callback function to perform the "yes" action.
        confirm("Continue", function () {
            alert("OK");
        });
    });
});

function confirm(message, callback) {
    $('#confirm').modal({
        close:false,
        position: ["20%",],
        overlayId:'confirmModalOverlay',
        containerId:'confirmModalContainer',
        onShow: function (dialog) {
            dialog.data.find('.message').append(message);

            // If the user clicks "yes"
            dialog.data.find('.yes').click(function () {
                $.get('my.php', function(data){
                    // Create a modal dialog with the data.
                    // Here: How do I write the same window?
                });

                // Call the callback

                // Close the dialog
                $.modal.close();
            });
        }
    });
}

这里我有一个问题,如何从 Ajax 结果中编写相同的窗口Confirmdialog。 我该怎么做?

I am using SimpleModal in jQuery, and I have one confirm dialog. If the result is Yes, I have to call my.php into this dialog. However, I have done the code, and I am still searching for ideas. How can I do it?

$(document).ready(function () {
    $('#confirmDialog input.confirm, #confirmDialog a.confirm').click(function (e) {
        e.preventDefault();
        // Example of calling the confirm function.
        // You must use a callback function to perform the "yes" action.
        confirm("Continue", function () {
            alert("OK");
        });
    });
});

function confirm(message, callback) {
    $('#confirm').modal({
        close:false,
        position: ["20%",],
        overlayId:'confirmModalOverlay',
        containerId:'confirmModalContainer',
        onShow: function (dialog) {
            dialog.data.find('.message').append(message);

            // If the user clicks "yes"
            dialog.data.find('.yes').click(function () {
                $.get('my.php', function(data){
                    // Create a modal dialog with the data.
                    // Here: How do I write the same window?
                });

                // Call the callback

                // Close the dialog
                $.modal.close();
            });
        }
    });
}

Here I have the problem of how to write it the same window Confirmdialog from an Ajax result. How can I do it?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

年华零落成诗 2024-07-18 11:24:52

我不确定确认功能最适合您的需求,但这样的功能应该可以工作:

function confirm(message, callback) {
    $('#confirm').modal({
        close:false,
        position: ["20%",],
        overlayId:'confirmModalOverlay',
        containerId:'confirmModalContainer',
        onShow: function (dialog) {
            dialog.data.find('.message').append(message);

            // If the user clicks "yes"
            dialog.data.find('.yes').click(function () {
                $.get("my.php", function (data) {
                    /* Sample response:
                     *   <div id="title">my title</div>
                     *   <div id="message">my message</div>
                     *
                     */
                    var resp = $("<div/>").append(data);
                    var title = resp.find("#title").html(),
                        message = resp.find("#message").html();

                    dialog.data.find(".header span").html(title);
                    dialog.data.find(".message").html(message);
                    dialog.data.find(".buttons .yes").hide();
                    dialog.data.find(".buttons .no").html("Close");

                    // No need to call the callback or $.modal.close()
                });
            });
        }
    });
}

I'm not sure that the confirm function best fits your needs, but something like this should work:

function confirm(message, callback) {
    $('#confirm').modal({
        close:false,
        position: ["20%",],
        overlayId:'confirmModalOverlay',
        containerId:'confirmModalContainer',
        onShow: function (dialog) {
            dialog.data.find('.message').append(message);

            // If the user clicks "yes"
            dialog.data.find('.yes').click(function () {
                $.get("my.php", function (data) {
                    /* Sample response:
                     *   <div id="title">my title</div>
                     *   <div id="message">my message</div>
                     *
                     */
                    var resp = $("<div/>").append(data);
                    var title = resp.find("#title").html(),
                        message = resp.find("#message").html();

                    dialog.data.find(".header span").html(title);
                    dialog.data.find(".message").html(message);
                    dialog.data.find(".buttons .yes").hide();
                    dialog.data.find(".buttons .no").html("Close");

                    // No need to call the callback or $.modal.close()
                });
            });
        }
    });
}
梨涡 2024-07-18 11:24:52

我不确定您想要完成什么——您是否尝试重用确认模式对话框来显示您的结果? 我想你可以这样做,因为你在对话框上有一个“关闭 X”按钮,只需用你的结果替换消息的内容并删除按钮,这样你的回调就不会再次被触发。 它可能看起来像这样:

 dialog.data.find('.message').html( 'new contents from your ajax data' );
 dialog,data.find('.buttons').remove();

但是,对我来说,这似乎是对模式对话框的滥用。 在我看来,对话框应该只包含与用户的一次交互。 如果您需要根据初始对话框的结果进行进一步交互,那么我会考虑添加另一个模式对话框,在当前对话框关闭后弹出,并包含 AJAX 结果,或者将 AJAX 结果插入主界面并处理它那里。

I'm not sure what you are trying to accomplish -- are you trying to reuse the confirmation modal dialog to display your results? I suppose you could do this, given that you have a "close X" button on the dialog by simply replacing the contents of the message with your results and removing the buttons so your callback doesn't get fired again. It might look something like this:

 dialog.data.find('.message').html( 'new contents from your ajax data' );
 dialog,data.find('.buttons').remove();

However, this seems like an abuse of a modal dialog to me. In my opinion, the dialog should only contain a single interaction with the user. If you need further interaction based on the results of your initial dialog, then I would consider either adding another modal dialog that you pop up after the current one is closed with your AJAX results or insert the AJAX results into your main interface and deal with it there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文