2014-03-28 64 views
2

想知道,是否可以創建多個選項的警報?具有多個選項的警報

例如,在Facebook中,當您尚未輸入完消息時嘗試關閉標籤頁/窗口時,會彈出一個選項「離開此頁面」和「保留在此頁面上」的提示。

+1

可能重複http://stackoverflow.com/questions/2054082/ javascript-alert-with-3-buttons) – Andy

+0

@Andy但是你不能命名'confirm()'選項。 – chris97ong

+0

@Andy那麼Facebook網站的創建者是如何做到的?他是如何給出警報的兩個選項,除了「好」和「取消」之外還有其他名字? – chris97ong

回答

2

實施例與形式,you'are尋找window.onbeforeunload:

<script type="text/javascript"> 
var originalFormContent 
var checkForChanges = true; 

jQuery(document).ready(function() { 
    originalFormContent = jQuery('#myForm input[type=text]').serialize() +  jQuery('#myForm select').serialize(); 
}); 

function onClose() { 

    if (checkForChanges && originalFormContent != "undefined") { 
     var content = jQuery('#myForm input[type=text]').serialize() + jQuery('#myForm select').serialize(); 
     if (content != originalFormContent) { 

      return confirm('You have unsaved changes. Click OK if you wish to continue ,click Cancel to return to your form.'); 
     } 
    } 

} 

window.onbeforeunload = onClose(); 

[3個按鈕JavaScript警告(的