关闭窗口前显示提示
Posted
技术标签:
【中文标题】关闭窗口前显示提示【英文标题】: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;
提前致谢。
【问题讨论】:
您之前是否尝试过输出result
:resultVar = result;
和console.log(result)
?
没有登录控制台
您是否尝试过更新您的 msgBox 版本?
【参考方案1】:
在您的 showPrompt()
函数中,resultVar
从回调中获取其值,但在执行回调之前,该函数会立即返回,这就是为什么 resultVar
在 showPrompt()
时仍然是 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
的行为不像 alert
或 confirm
:调用时,它不会挂起当前执行线程。
您的函数在用户单击任何按钮之前返回。
解决此问题的最简单方法是从“成功”回调中调用您的 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)
。以上是关于关闭窗口前显示提示的主要内容,如果未能解决你的问题,请参考以下文章