simplemodal确认示例等待返回值

时间:2014-10-01 10:50:00

标签: javascript simplemodal

我遇到simpleModal确认示例的问题。我想等待结果作为基本确认对话框,然后根据按下的按钮采取行动。我在其他的js打电话。我的js代码是:

 var res = confirm("Delete elements?");

 if (res == true) {
       mainController.deleteNode(nodeMoveMessage, url);
  }

和进行对话的js是:

function confirm(message, callback) {
var modalWindow = document.getElementById("confirm");
console.log(modalWindow);
$(modalWindow).modal({
    closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
    position: ["20%",],
    overlayId: 'confirm-overlay',
    containerId: 'confirm-container',
    onShow: function (dialog) {
        var modal = this;

        $('.message', dialog.data[0]).append(message);

        // if the user clicks "yes"
        $('.yes', dialog.data[0]).click(function () {
            // close the dialog
            modal.close(); // or $.modal.close();
            // call the callback
            return true;
        });
    }
});
}

我的jsp中的div是:

    <div id='confirm'>
        <div class='header'><span>Confirm</span></div>
        <div class='message'></div>
        <div class='buttons'>
            <div class='no simplemodal-close'>No</div><div class='yes'>Yes</div>
        </div>
    </div>

非常感谢!

1 个答案:

答案 0 :(得分:0)

你必须为你的模态添加一个回调,所以首先在confirm函数中添加它:

function confirm(message, callback) {
    var modalWindow = document.getElementById("confirm");
    console.log(modalWindow);
    $(modalWindow).modal({
        closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
        position: ["20%",],
        overlayId: 'confirm-overlay',
        containerId: 'confirm-container',
        onShow: function (dialog) {
            var modal = this;

            $('.message', dialog.data[0]).append(message);

            $('.yes', dialog.data[0]).click(function () {
                modal.close(); $.modal.close();
                callback(); // HERE IS THE CALLBACK
                return true;
            });
        }
    });
}

然后你可以用点击 yes 按钮时执行的回调来调用confirm函数,如下所示:

function doSomething() {
    mainController.deleteNode(nodeMoveMessage, url);
}

confirm("Delete elements?", doSomething);

现在,当用户点击按钮时,将调用doSomething函数并删除元素。