【问题标题】:display prompt before closing the window关闭窗口前显示提示
【发布时间】:2013-12-10 13:25:09
【问题描述】:

我在显示警报窗口时遇到了一些问题。我想要的是显示警报窗口,然后根据用户的输入我需要执行一些操作。但我面临的问题是我从函数接收到 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;

}

提前致谢。

【问题讨论】:

  • 您之前是否尝试过输出resultresultVar = result;console.log(result)
  • 没有登录控制台
  • 您是否尝试过更新您的 msgBox 版本?

标签: javascript jquery alerts msgbox


【解决方案1】:

在您的 showPrompt() 函数中,resultVar 从回调中获取其值,但在执行回调之前,该函数会立即返回,这就是为什么 resultVarshowPrompt() 时仍然是 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);
    });
}

【讨论】:

  • 嗨 Jonhopkins,我已经尝试过了,但仍然无法控制function(result)
【解决方案2】:

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

【讨论】:

  • 嗨,LeGEC,我已经这样做了,但仍然无法正常工作。控制永远不会进入function(result){}
猜你喜欢
  • 2013-11-04
  • 1970-01-01
  • 2017-01-27
  • 1970-01-01
  • 1970-01-01
  • 2013-09-15
  • 2012-02-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多