【问题标题】:SimpleModal confirmation and proceeding to original hrefSimpleModal 确认并继续原始href
【发布时间】:2012-01-15 23:40:16
【问题描述】:

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

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

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

但是,我想将 SimpleModal 用于警报窗口。如果我做任何事情除了,我可以让 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呢?

谢谢!

【问题讨论】:

    标签: jquery simplemodal confirm confirmation


    【解决方案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);
                });
            }
        });
    }
    

    【讨论】:

    • 谢谢你,罗里。我不得不修改一点点。 prop('src") 和 attr('src') 都不起作用。但是,prop('href') 确实起作用。
    • 对不起 - 这是一个愚蠢的错误,我已经编辑了答案。如果这解决了您的问题,您应该将其标记为已接受的答案。
    猜你喜欢
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2011-03-30
    • 1970-01-01
    • 2014-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多