2

私はalertこのようにfancyboxですべてを拡張しました

window.alert = function(message){
    $("#alert_message_box .warmsg span").html(message);
    $.fancybox({'href':'#alert_message_box'});
    return false;
}

そして、その正常に動作します。confirm私もボックスのように拡張しようとします

 window.confirm = function(message){
    $("#confirm_message_box .warmsg span").html(message);
    $.fancybox({'href':'#confirm_message_box'});
}

confirm_message_boxdiv には 2 つのボタンが含まれYesNo.

if(confirm(alert_msg)){ } この場合、関数がどのように呼び出され、呼び出された関数にどのようにconfirm戻ることができますかtruefalse

これらの関数を上書きすることに問題があるかどうかはわかりません。

これはjqueryを使用したjsfiddleデモです(ここではfancyboxは使用されていません)

http://jsfiddle.net/fzTa3/

4

2 に答える 2

1

この場合、確認の Fancybox バージョンはモーダル ダイアログではないため、ワークフローを変更する必要があります (スクリプトの実行を一時停止しません)。対応するボタンのクリックでコールバックを呼び出すには、コールバックを使用する必要があります。

window.confirm = function(message, onYes, onNo) {
    $("#confirm_message_box .warmsg span").html(message);
    $.fancybox({
        href: '#confirm_message_box'
        afterLoad: function() {
            this.inner.find('.btn-yes').click(onYes);
            this.inner.find('.btn-no').click(onNo);
        }
    });
}

onYes関数をYesボタンとファンシーボックスの初期化onNoにバインドする必要がありNoます。

そして、使用法は次のようになります。

confirm('Are you sure?', function() {
    // he is sure
}, function() {
    // cancel something
});
于 2013-03-13T06:45:05.040 に答える
0

@dfsqの答えに同意しないわけではありませんが、私自身がこれに取り組んでいる間、Yes, Noコールバックのアプローチが制限的すぎることがわかりました。

使用例:

confirm("Do you wish to continue?", 
    {
        /* 
        Define your buttons here, can handle multiple buttons 
        with custom title and values
        */
        buttons: [
            { class: "yes", type: "button", title: "Yes", value: "yes" },
            { class: "no", type: "button", title: "No", value: "no" },
            { class: "maybe", type: "button", title: "Maybe?", value: "maybe" }
        ],
        modal: true
    }, 
    function(resp) {
        alert("You clicked " + resp);
    }
);
于 2014-08-04T08:31:10.467 に答える