关闭窗口前显示提示
display prompt before closing the window
我在显示警报窗口时遇到一些问题。我想要的是显示警报窗口,然后根据用户的输入,我需要执行一些操作。但我面临的问题是,我从函数中接收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;
}
提前谢谢。
在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);
});
}
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);
}
相关文章:
- 单元格的工具提示或标题不显示超过2000个字符
- 如何更改已显示的工具提示
- 显示数字键盘的Phonegap-js提示
- 如何在jQuery点击函数中添加并显示引导工具提示
- 如何在将鼠标悬停在提交/锚点上时检查复选框是否已选中,并显示提示用户这样做的警报
- 双击谷歌地图显示提示框不起作用
- 关闭窗口前显示提示
- 在 Vaadin 7 中创建的 onbeforeunload 函数返回空字符串在 IE 10 中仍然显示提示
- 脚本中未显示提示
- 悬停时显示提示消息
- 如何在刷新页面时显示提示消息
- Javascript 不显示提示
- 如何使用JavaScript在刽子手游戏中还剩3个猜测(生命)时显示提示
- 在Angular中隐藏/显示提示文本
- Html, javascript:滚动列表框项目-并为每个项目显示提示
- 24小时内仅点击一次,显示提示框
- JQuery中的自动完成不显示提示
- 如何在元素附近的适当位置显示提示
- 如何在 html 表单域中显示提示
- jquery ui自动完成显示提示