2013-12-10 174 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