jQuery中的简单模态

时间:2008-12-27 12:12:06

标签: jquery ajax popup simplemodal

我在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。我该怎么办?

2 个答案:

答案 0 :(得分:2)

我不确定确认功能最适合您的需求,但这样的事情应该有效:

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()
                });
            });
        }
    });
}

答案 1 :(得分:1)

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

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

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