我如何使用简单模式来动态确认弹出窗口

发布于 2024-07-30 12:36:19 字数 309 浏览 1 评论 0原文

我正在使用简单的模型,这是一段非常简洁的代码,但我有一个我无法弄清楚的要求。

http://www.ericmmartin.com/simplemodal/

我的用例是第三个选项,我想要在用户单击某个操作后出现“确认弹出窗口”。 问题在于,在示例中,消息被硬编码在 js 文件中。

我需要能够传递此消息以及与“是”和“否”按钮关联的链接。

有没有人做过类似的事情。

I am using simple model which is a very neat piece of code but i have one requirement i can't figure out.

http://www.ericmmartin.com/simplemodal/

my use case is the third options where i want a "Confirmation Popup" after a user clicks on an action. The issue is that in the example the message is hardcoded in the js file.

i need to be able to pass in this message as well as the link that is associated with the "Yes" and "no" buttons.

has anyone done anything similar.

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

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

发布评论

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

评论(3

忱杏 2024-08-06 12:36:19

查看页面的源代码会告诉您需要了解的一切。

<!-- Confirm -->
<link type='text/css' href='css/confirm.css' rel='stylesheet' media='screen' />
<script src='js/confirm.js' type='text/javascript'></script>

上面

<div id='confirmDialog'><h2>Confirm Override</h2>

    <p>A modal dialog override of the JavaScript confirm function. Demonstrates the use of <code>onShow</code> as well as how to display a modal dialog confirmation instead of the default JavaScript confirm dialog.</p>
    <form action='download/' method='post'>
        <input type='button' name='confirm' value='Demo' class='confirm demo'/><input type='button' name='download' value='Download' class='demo'/>
        <input type='hidden' name='demo' value='confirm'/>
    </form>
</div>
<div id='confirm' style='display:none'>

    <a href='#' title='Close' class='modalCloseX simplemodal-close'>x</a>
    <div class='header'><span>Confirm</span></div>
    <p class='message'></p>
    <div class='buttons'>
        <div class='no simplemodal-close'>No</div><div class='yes'>Yes</div>
    </div>
</div>

我们可以清楚地看到消息传递全部在 HTML 中,而不是在 javascript 中。

然后,如果我们查看confirm.js 的JS 源代码,那么它就已经为您准备好了如何初始化/触发它。

/*
 * SimpleModal Confirm Modal Dialog
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2009 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id: confirm.js 185 2009-02-09 21:51:12Z emartin24 $
 *
 */

$(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 to the SimpleModal Project page?", function () {
            window.location.href = 'http://www.ericmmartin.com/projects/simplemodal/';
        });
    });
});

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 () {
                // call the callback
                if ($.isFunction(callback)) {
                    callback.apply();
                }
                // close the dialog
                $.modal.close();
            });
        }
    });
}

Looking at the source of the page tells you everything you need to know.

<!-- Confirm -->
<link type='text/css' href='css/confirm.css' rel='stylesheet' media='screen' />
<script src='js/confirm.js' type='text/javascript'></script>

and

<div id='confirmDialog'><h2>Confirm Override</h2>

    <p>A modal dialog override of the JavaScript confirm function. Demonstrates the use of <code>onShow</code> as well as how to display a modal dialog confirmation instead of the default JavaScript confirm dialog.</p>
    <form action='download/' method='post'>
        <input type='button' name='confirm' value='Demo' class='confirm demo'/><input type='button' name='download' value='Download' class='demo'/>
        <input type='hidden' name='demo' value='confirm'/>
    </form>
</div>
<div id='confirm' style='display:none'>

    <a href='#' title='Close' class='modalCloseX simplemodal-close'>x</a>
    <div class='header'><span>Confirm</span></div>
    <p class='message'></p>
    <div class='buttons'>
        <div class='no simplemodal-close'>No</div><div class='yes'>Yes</div>
    </div>
</div>

Above we can clearly see that the messaging is all in the HTML, and not in the javascript at all.

And if we then look at the JS source of confirm.js it's all laid out there for you in terms of how to initialize/trigger it.

/*
 * SimpleModal Confirm Modal Dialog
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2009 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id: confirm.js 185 2009-02-09 21:51:12Z emartin24 $
 *
 */

$(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 to the SimpleModal Project page?", function () {
            window.location.href = 'http://www.ericmmartin.com/projects/simplemodal/';
        });
    });
});

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 () {
                // call the callback
                if ($.isFunction(callback)) {
                    callback.apply();
                }
                // close the dialog
                $.modal.close();
            });
        }
    });
}
情域 2024-08-06 12:36:19

我会推荐我使用的 BlockUI。 使用此插件,它使用现有的

值来显示消息。 如果触发对话框,您可以使用 jQuery 通过正常的 DOM 操作修改消息和链接文本,然后再根据应用程序的需要显示

jQuery BlockUI 插件 - 对话框示例

编辑 - 根据下面的第一条评论。

<script type="text/javascript"> 
$(document).ready(function() { 

    $('#test').click(function() { 
        $.blockUI({ message: $('#question'), css: { width: '275px' } }); 
    }); 

    $('#yes').click(function() { 
        // Remove the UI block.
        $.unblockUI(); 
        //  Or you could use window.open
        window.location = "http://www.google.com";
    }); 

    $('#no').click(function() { 
        $.unblockUI(); 
        return false; 
    }); 
}); 

I would recommend BlockUI which is what I use. With this plugin, it uses an existing <div> value to display the message. In the event that triggers the dialog to fire you can use jQuery to modify the message and link text via normal DOM manipulation before it shows the <div> as your application requires.

jQuery BlockUI Plugin - Dialog Example

EDIT - per the first comment below.

<script type="text/javascript"> 
$(document).ready(function() { 

    $('#test').click(function() { 
        $.blockUI({ message: $('#question'), css: { width: '275px' } }); 
    }); 

    $('#yes').click(function() { 
        // Remove the UI block.
        $.unblockUI(); 
        //  Or you could use window.open
        window.location = "http://www.google.com";
    }); 

    $('#no').click(function() { 
        $.unblockUI(); 
        return false; 
    }); 
}); 

一江春梦 2024-08-06 12:36:19

inform.js 中给出的代码包含两个方法定义。 一种是名为 confirm 的通用方法,它将使用您想要显示的消息创建模式弹出窗口。 每当您想要创建模式弹出窗口时,您都必须使用此方法。

confirm("Are you sure you want to delete this item?", function() {
    //Here you will write the code that will handle the click of the OK button.
});

这里,第二个参数是一个函数(这几乎像 C# 中的委托一样工作)。 将会发生的情况是,confirm 函数将显示一个包含您的消息的对话框,并且当用户单击任何按钮时,将调用作为第二个参数传递的匿名函数。 您还可以编写一个“普通”函数并将其作为第二个参数传递给 confirm -

function callbackHandler() {
    //Here you will write the code that will handle the click of the OK button.
}

confirm("Are you sure you want to delete this item?", callbackHandler);

您的函数将由这段代码调用 -

// if the user clicks "yes"
dialog.data.find('.yes').click(function () {
    // call the callback
    if ($.isFunction(callback)) { callback.apply(); }
    // close the dialog
    $.modal.close();
});

The code given in the confirm.js contains two method definitions. One is a generic method called confirm, which will create your modal popup with the message you want to be displayed. You have to use this method whenever you want to create the modal popup.

confirm("Are you sure you want to delete this item?", function() {
    //Here you will write the code that will handle the click of the OK button.
});

Here, the second argument is a function (this works almost like a delegate in C#). What will happen is that the confirm function will show a dialog containing your message, and when the user clicks any button, the anonymous function passed as the second argument will be invoked. You can also write a "normal" function and pass it as a second argument to confirm -

function callbackHandler() {
    //Here you will write the code that will handle the click of the OK button.
}

confirm("Are you sure you want to delete this item?", callbackHandler);

Your function will be invoked by this piece of code -

// if the user clicks "yes"
dialog.data.find('.yes').click(function () {
    // call the callback
    if ($.isFunction(callback)) { callback.apply(); }
    // close the dialog
    $.modal.close();
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文