2

検索しましたが、解決策が見つかりません。うまくいけば、私は何か単純なものを見落としています。

私が求めているのは、標準のjquery確認です。

$('.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 に答える