2013-12-10 170 views
0

我正面臨着顯示警告窗口的一些問題。我想要的是顯示警報窗口,然後根據用戶的輸入我需要執行一些操作。但是我面臨的問題是,我從函數接收null作爲返回值。關閉窗口之前顯示提示

PS:我正在使用Jquery msgbox v1.0進行警報。

這裏調用函數的代碼塊 -

var retVal = "No"; //don't save 
    if($(this).parent().parent().hasClass("modified")){ 
     retVal = showPrompt();//Null returned 
    } 
    alert(retVal); 
    switch (retVal) { 
    case "Yes": 
     //show download xml file on machine.    
     removeWorkspace(this); 
     break; 
    case "No": 
     removeWorkspace(this); 
     break; 
    case "Cancel": 
     //don't do anything 
     break;  
    } 
    event.stopPropagation(); 
}); 

被調用函數:

function showPrompt(){ 
var resultVar = null; 
$.msgBox({ 
    title: "Are you sure", 
    content: "Do you want to save your work?", 
    type: "confirm", 
    buttons: [{ type:"submit", value: "Yes"}, 
      {type: "submit", value: "No"}, 
      {type: "cancel", value: "Cancel"}] 
}, function(result){ 
    resultVar = result; 
}); 

    return resultVar; 

} 

在此先感謝。

+0

您是否曾嘗試在'resultVar = result;'之前用'console.log(result)'輸出'result'? –

+0

沒有登錄控制檯 – codeomnitrix

+0

你試過更新你的msgBox版本嗎? –

回答

0

msgBox不表現爲alertconfirm:被調用時,它不暫停當前執行線程。

您的函數在用戶點擊任何按鈕之前返回。

解決這一問題將是從「成功」回調打電話給你removeWorkspace功能最簡單的方法:

function showPrompt(ws){ 
    $.msgBox({ 
     title: "Are you sure", 
     content: "Do you want to save your work?", 
     type: "confirm", 
     buttons: [{ type:"submit", value: "Yes"}, 
        {type: "submit", value: "No"}, 
        {type: "cancel", value: "Cancel"}] 
    , success: function(result){ 
     switch result { 
      case "Yes" : 
       removeWorkspace(ws); 
       break; 
      case "No" : 
       removeWorkspace(ws); 
       break; 
     } 
    }}); 
} 

// change the calling site : 
if($(this).parent().parent().hasClass("modified")){ 
    showPrompt(this); 
} else { 
    // default action : 
    removeWorkspace(this); 
} 
+0

嗨LeGEC,我已經做到了這一點,但仍然無法正常工作。控制永遠不會進入'function(result){}'。 – codeomnitrix

+0

它現在工作.. :) – codeomnitrix

1

在你showPrompt()功能,resultVar從回調獲取其值,但功能正在在執行回調之前立即返回,這就是爲什麼當showPrompt()返回時resultVar仍然是null

而不是試圖按照它的方式運行開關,爲什麼不將它移動到另一個從回調中調用的函數?

var retVal = "No"; //don't save 
var that = this; 
if($(this).parent().parent().hasClass("modified")){ 
    showPrompt(); 
} else { 
    methodWithSwitch(retVal); 
} 
event.stopPropagation(); 

function methodWithSwitch(val) { 
    alert(val); 
    switch (val) { 
    case "Yes": 
     //show download xml file on machine.    
     removeWorkspace(that); 
     break; 
    case "No": 
     removeWorkspace(that); 
     break; 
    case "Cancel": 
     //don't do anything 
     break;  
    } 
} 

function showPrompt(){ 
    $.msgBox({ 
     title: "Are you sure", 
     content: "Do you want to save your work?", 
     type: "confirm", 
     buttons: [{ type:"submit", value: "Yes"}, 
       {type: "submit", value: "No"}, 
       {type: "cancel", value: "Cancel"}] 
    }, function(result){ 
     methodWithSwitch(result); 
    }); 
} 
+0

嗨Jonhopkins,我試過這個,但仍然控制永遠不會進入'功能(結果)' – codeomnitrix

+0

它現在工作.. :) – codeomnitrix