2

我已经搜索并找不到解决方案。希望我忽略了一些简单的事情。

我所追求的是一个标准的 jquery 确认,一个 la:

$('.confirm').click(function(){
    var answer = confirm('Delete '+jQuery(this).attr('title'));
    return answer // answer is a boolean
});    

但是,我想将 SimpleModal 用于警报窗口。如果在 SimpleModal 对话框中单击“是”,除了尝试让原始 href 继续进行之外,我可以让 SimpleModal 出现并工作。

目前的代码......

jQuery(function ($) {

$('.confirm').click(function (e) {
    e.preventDefault();
    confirm("", function () {
        //alert('yes');
        $('.confirm').trigger('click');
    });
});
});

function confirm(message, callback) {
$('#confirm').modal({
    closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
    position: ["20%",],
    overlayId: 'please-wait',
    containerId: 'confirm-container', 
    onShow: function (dialog) {
        var modal = this;
        $('.message', dialog.data[0]).append('Delete '+$('a.confirm').attr('title'));

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

我还在确认功能中注释掉了一个警报。警报确实有效。但是如何让浏览器继续原来的href?

谢谢!

4

1 回答 1

1

首先,您需要修改您的点击处理程序以传递链接的 url:

jQuery(function ($) {
    $('.confirm').click(function (e) {
        e.preventDefault();
        confirm($(this).prop("href"), "", function () {
            $('.confirm').trigger('click');
        });
    });
});

如果您使用的是 jQuery 1.6 或更低版本,请使用$(this).attr("href")而不是.prop()

然后修改函数以使用该 url:

function confirm(url, message, callback) {
    $('#confirm').modal({
        closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
        position: ["20%",],
        overlayId: 'please-wait',
        containerId: 'confirm-container', 
        onShow: function (dialog) {
            var modal = this;
            $('.message', dialog.data[0]).append('Delete '+$('a.confirm').attr('title'));

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

                // transfer to the url:
                window.location.assign(url);
            });
        }
    });
}
于 2011-12-08T14:41:50.777 回答