2013-03-13 60 views
2

我伸出我所有的alert像這樣擴展的Javascript確認框用的fancybox

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

其做工精細的fancybox。我也儘量延長confirm盒狀

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

confirm_message_box格包含兩個按鈕,YesNo

if(confirm(alert_msg)){ } 在這種情況下如何confirm功能將被調用,以及如何我可以返回truefalse被調用函數

我不知道有沒有覆蓋這些功能的問題。

這裏是一個的jsfiddle演示使用jQuery(這裏未使用的fancybox)

http://jsfiddle.net/fzTa3/

回答

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功能結合到按鈕,onNo沒有在你的fancybox初始化。

而且用法是這樣的:

confirm('Are you sure?', function() { 
    // he is sure 
}, function() { 
    // cancel something 
}); 
+0

thats cuuul idea ..請問您能解釋一下,我怎樣才能調用回調方法。在這種情況下,我對所有「是」使用相同的按鈕「否」 – 2013-03-13 06:50:03

+0

請參閱我的編輯,如何將回調函數綁定到按鈕。 – dfsq 2013-03-13 06:54:50

+0

可以請你重新寫一下嗎? http://jsfiddle.net/fzTa3/ not used fancybox here – 2013-03-13 06:58:10

0

不要不同意@dfsq's answer,但我覺得是,否回調接近過於嚴格,而這方面的工作我發現了一個更靈活的方式來處理這個。

用法示例:

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); 
    } 
);