【发布时间】:2014-08-25 17:00:09
【问题描述】:
我正在使用 JQuery 和 JQuery UI,我尝试使用 JQuery UI Dialog 来解决这个问题,但我无法完成。
基本上我有例如这个使用 JS 确认框的代码:
// if user presses cancel, do not proceed to the next section of the code
if ( ! confirm('Are you sure you want to press OK?')) {
return;
}
.... Lots of code below
我想将其转为使用可以使用 CSS 自定义的自定义框。我尝试了 UI 对话框:
function confirmIt(text) {
$('#confirm_dialog').dialog({
width: 300,
height: 150,
buttons: {
"OK": function() { return true; },
"Cancel": function() { return false; }
}
});
}
if ( ! confirmIt('Are you sure you want to press OK?')) {
return;
}
.... Lots of code below
现在上面代码的问题是 if 条件下的很多代码仍然执行。我知道 JQuery Dialog 是 aync 进程,但我想知道是否有这样的免费插件,我可以使用它来返回布尔值?基本上功能方面它应该与 JS confirm() 完全相同。我不想在 JQuery UI 对话框的回调中包装大量代码来实现这一点。如果CANCEL is clicked,我只想停止脚本执行,如果OK is clicked,我只想继续执行。
【问题讨论】:
-
这是不可能的,因为这些对话框有“回调”,会在某些操作上被调用(例如单击“确定”)。原生方法
prompt、alert和confirm将等待用户输入并暂停脚本执行 -
@revaxarts 没有办法像 JS 原生方法一样暂停脚本执行?
-
我已经在我的回答中解释了一个可能的解决方案
-
关于您刚才删除的问题,请考虑在 SO 聊天室中询问硕士课程。
标签: jquery jquery-ui jquery-ui-dialog