2014-01-18 45 views
0

我想用jQuery對話框覆蓋javascript確認。這裏是我的overridin代碼:
用jquery覆蓋javascript確認

window.confirm = function(message, caption = 'Confirmation'){ 
    $(document.createElement('div')).attr({title: caption, 'class': 'dialog'}).html(message).dialog({ 
     position:['center',100], 
     dialogClass: 'fixed', 
     buttons: { 
      "OK": function(){ 
       $(this).dialog('close'); 
       return true; 
      }, 
      "Cancel": function(){ 
       $(this).dialog('close'); 
       return false; 
      } 
     }, 
     close: function(){ 
      $(this).remove(); 
     }, 
     draggable: false, 
     modal: true, 
     resizable: false, 
     width: 'auto' 
    }); 
}; 

這裏是我的動作代碼:

if(confirm('Are you sure?') == false) { return false; } 

此代碼不能正常工作。我怎樣才能做到這一點?

+0

你的意思是什麼不起作用?它是否在控制檯上拋出一些js錯誤? – dreamweiver

+0

對於它的工作,函數'confirm()'只有在彈出被解除後才能返回*。從彈出式按鈕事件處理程序中返回true/false不會將其剪切。 – techfoobar

+0

它不會按預期工作,因爲該對話框異步執行...在你的情況下,確認沒有返回任何東西(未定義)..解決方案是使用回調方法 –

回答

6

這是因爲確認方法顯示和對話框,並且它在按鈕被按下之前返回。

您可以使用一個回調方法來解決它

window.confirm = function (message, callback, caption) { 
    caption = caption || 'Confirmation' 

    $(document.createElement('div')).attr({ 
     title: caption, 
      'class': 'dialog' 
    }).html(message).dialog({ 
     position: ['center', 100], 
     dialogClass: 'fixed', 
     buttons: { 
      "OK": function() { 
       $(this).dialog('close'); 
       callback() 
       return true; 
      }, 
       "Cancel": function() { 
       $(this).dialog('close'); 
       return false; 
      } 
     }, 
     close: function() { 
      $(this).remove(); 
     }, 
     draggable: false, 
     modal: true, 
     resizable: false, 
     width: 'auto' 
    }); 
}; 

confirm('dd', function() { 
    //what every needed to be done on confirmation has to be done here 
    console.log('confirmed') 
}) 

演示:Fiddle

你不能if..else語句中使用它

0

我知道是一個老問題,但對於completness我離開了我解決方案,是一個jQuery插件,你只需要複製/粘貼這個內容保存爲.js文件幷包含(和jquery-ui一起)到你的html中,並且所有的alert和confirm對話框都被替換。

我必須指出的是上述方案僅要求用戶成功(按OK鍵)的回調,但我想總是被調用的回調,這種方式可以實現更多行爲

jQuery.cambiarAlert = function (options) 
{ 
    var defaults = { 
     title: "Atención", 
     buttons: { 
      "Aceptar": function() 
      { 
       jQuery(this).dialog("close"); 
      } 
     } 
    }; 

    jQuery.extend(defaults, options); 

    delete defaults.autoOpen; 


    window.alert = function() 
    { 
     var html; 

     try { 
       html = arguments[0].replace(/\n/, "<br />") 
      } catch (exception) { 
       html = arguments[0] 
     } 

     jQuery("<div />", { 
          html: "<div class='.navbar-inverse .navbar-inner'>" + html + "</div>" 
     }).dialog(defaults); 
    }; 

    window.confirm = function (message, callback, caption) { 
     caption = caption || 'Confirmación' 

     $(document.createElement('div')).attr({ 
      title: caption, 
      'class': 'dialog' 
     }).html(message).dialog({ 
      buttons: { 
       "Aceptar": function() { 
        $(this).dialog('close'); 
        if (callback && typeof(callback) == "function"){ 
         callback(true); 
        } 
        return true; 
       }, 
       "Cancelar": function() { 
        $(this).dialog('close'); 
        if (callback && typeof(callback) == "function"){ 
         callback(false); 
        } 
        return false; 
       } 
      }, 
      close: function() { 
       $(this).remove(); 
      }, 
      draggable: false, 
      modal: true, 
      resizable: false, 
      width: 'auto' 
     }).position({ 
      my: "center", 
      at: "center", 
      of: window 
     }); 
    }; 

    return this; 
}; 




$(function() 
{ 
    $.cambiarAlert(); 
}); 

點這裏是,我做的調用回調函數(真)或回調(假)這樣我可以編寫使用結果作爲函數本身

的參數的回調函數,所以在我的代碼,我可以這樣做:

confirm("Are you sure?", function(result){ 
    if (result){ 
     //Perform ajax call 
    } 
})