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

标签 javascript jquery variables simplemodal

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

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

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

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

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

最佳答案

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

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

关于javascript - 我如何使用简单模式来动态确认弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1239566/

相关文章:

javascript - 同侧的两个菜单

javascript - 如何在 Kendo UI mobile 中不使用 app.navigate() 来切换 View ?

javascript - 返回同一页面后如何重置复选框,然后在浏览器中按“返回”

javascript - 如何将变量的内容传递给jquery函数

javascript - 字符串变量不接受 "?"符号

javascript - 确保带有异步调用的 forEach 在另一个调用之前执行?

javascript - Angular.js指令模板: break line

javascript - jQuery 表中的表长度

javascript - jQuery:给下拉菜单一个高度

javascript - 设置变量 if...then javascript