如何让代码等待确定/取消按钮选择?
我正在使用 JQuery 的 $.dialog()
,我在其中打开带有“确定”和“取消”按钮的对话框。
我预计当对话框打开时,代码会停止,并在用户选择“确定”或“取消”时首先继续。
这是我的完整源代码 http://pastebin.com/uw7bvtn7
我遇到问题的部分位于第 127-151 行。
$("#dialog:ui-dialog").dialog("destroy");
$("#dialog-confirm").dialog({
resizable: false,
height: 600,
modal: true,
open: function() {
$(this).children('div.dialog-text').replaceWith("<h3><b>Users</b></h3>" + makeDialogTable(users) + "<h3><b>Owners</b></h3>" + makeDialogTable(owners));
},
buttons: {
"Okay": function() {
$(this).dialog("close");
},
Cancel: function() {
is_okay = 0;
$(this).dialog("close");
}
} // buttons
}); // dialog
alert(is_okay);
代码现在所做的就是首先显示对话框,然后在顶部显示 alert(is_okay)
。
我想要的是,当用户按下“确定”或“取消”时,代码首先继续。
怎么可能呢?
I am using JQuery's $.dialog()
, where I open a dialog with OK and Cancel buttons.
I would have expected that when the dialog opens, the code stops, and would first continue, when the user had selected OK or Cancel.
Here is my complete source code
http://pastebin.com/uw7bvtn7
The section where I have the problem is at line 127-151.
$("#dialog:ui-dialog").dialog("destroy");
$("#dialog-confirm").dialog({
resizable: false,
height: 600,
modal: true,
open: function() {
$(this).children('div.dialog-text').replaceWith("<h3><b>Users</b></h3>" + makeDialogTable(users) + "<h3><b>Owners</b></h3>" + makeDialogTable(owners));
},
buttons: {
"Okay": function() {
$(this).dialog("close");
},
Cancel: function() {
is_okay = 0;
$(this).dialog("close");
}
} // buttons
}); // dialog
alert(is_okay);
What the code does right now is to first show the dialog and then the alert(is_okay)
on top.
What I would like is that the code first continues when the user have pressed OK or Cancel.
How could that be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
代码确实按顺序执行。
$("#dialog-confirm").dialog()
的作用是弹出一个对话框。alert(is_okay)
在上面的行执行完毕之前不会执行。但Okay
和Cancel
是事件侦听器。上面的代码将事件侦听器分配给事件。这就是它所做的一切,它不执行这些函数,它只是将这些函数分配给事件调用。
我建议阅读一些有关事件和事件侦听器的内容。如果您打算认真使用 JQuery,它将为您省去很多困惑。
The code does execute sequentially. The job of
$("#dialog-confirm").dialog()
is to popup a dialog box.alert(is_okay)
won't execute until the lines above it have been executed. ButOkay
andCancel
are event listeners.The code above assigns event listeners to events. That's all it does, it does not execute those functions, it just assigns those functions to event calls.
I would recommend doing some reading on events and event listeners. If you plan on using JQuery seriously, it will save you a lot of confusion.
或者您可以稍后绑定函数:
代码不会像
alert
那样停止并继续,但仅在对话框关闭时才会显示消息。Or you can bind function later :
The code doesn't stop and continue like it does with
alert
, but it will display message only when dialog is closed.只需将打开对话框后的所有代码放入一个函数中,然后从对话框回调函数中调用该函数即可确定/取消。
Simply put all code that follows after opening the dialog in a function and call that function from the dialogs callback function for okay / cancel.
浏览器使用基于事件的异步编程模型,其中许多事情可以(并且确实)同时发生。这就是样式转换(动画如滚动或淡入淡出)的工作原理。
您的示例显示一个对话框,然后引发警报。它不能“等待”用户单击按钮,因为这样做会阻止浏览器执行其他任何操作。
因此,您需要重构代码,以便在单击与操作关联的回调中的“确定”或“取消”按钮时执行您需要执行的操作。
换句话说,您需要:
Browsers use an event-based, asynchronous programming model where many things can (and do) occur at the same time. This is how style-transitions (animations like rolldown or fade) work.
Your example displays a dialog and then throws an alert. It cannot "wait" for the user to click on a button because doing so would stop the browser from doing anything else.
So you'll need to refactor your code to do whatever you require to happen when either the OK or Cancel buttons are clicked within the callback associated with the action.
In other words, you need to:
JavaScript 中没有直接的方法来“等待”某些事情——异步事件通常通过回调来处理。这意味着您需要根据事件而不是顺序代码来考虑您的程序。而不是您当前的代码:
您需要将“其余代码”部分包装到其自己的函数中,然后从对话框的“确定/取消”回调中调用该函数:
There is no direct way to "wait" for something in JavaScript -- asynchronous events are generally handled through callbacks. This means you need to think of your program in terms of events and not as sequential code. Instead of your current code:
you need to wrap the "rest of code" section into its own function, then call that from the OK/Cancel callbacks of your dialog:
因此,如果我理解正确的话,您希望显示一个带有“确定”和“取消”选项的
警报
,并且除非单击“确定”,否则不会出现对话框
。与其使用另一个
警报
,为什么不尝试使用另一个包含“确定”和“取消”的对话框
呢?在您的 html 中执行此操作:
使用此 css:
然后您可以在将创建
dialog
的事件上执行此操作(您想要等待的位置):$('#hiddenDialogElements').dialog ({
//代码
});
这是:
So if I understand correctly you want an
alert
to show up with the options OK and Cancel and theDialog
would not come up unless OK was hit.Instead of using another
alert
why not try using anotherdialog
with Ok and Cancel in it?In your html do this:
With this css:
Then you can do this on the event that will create the
dialog
(Where you want a wait):$('#hiddenDialogElements').dialog({
//Code
});
And this:
做这样的事情...
}
现在这样称呼它
你需要的一件事是 JQueryUI 插件...享受吧。
Do Something Like this ...
}
Now Call it like this
One thing u need is JQueryUI plugin... Enjoy.
您可以将附加代码放入“确定”和“取消”按钮功能中。例如:
You can put your additional code in the "Okay" and "Cancel" button functions. For example:
这不可能以“好的”方式完成,而且我强烈建议不要这样做。
您所描述的是一个完全阻塞的模态窗口/对话框,这对于 Web 应用程序来说非常棒。您已经使用
modal
标志创建了对话框,因此当对话框打开时,用户实际上无法在您的网站上执行任何操作,但 UI 会保持响应。同样,实际上没有办法“保持”代码执行。任何朝这个方向的方法都会冻结 UI 线程,因为 Javascript 和 UI 更新共享同一个线程。
多年来,开发人员推动 Javascript 变得越来越非阻塞(浏览器中的 Javascript 实际上总是遵循这条路线,这是非常好的事情)。这个想法在后端的 NodeJS 中重生。所以,你在这里逆流而上,不要这样做。
无论你试图解决什么问题,尝试以不同的方式思考。考虑函数式,使用回调和事件,考虑... ECMAscript! :p
That can't be done in a "good" manner plus I strongly recommend not to go that way.
What you describe is a complete blocking, modal window/dialog which is just aweful for web applications. You're already creating the dialog with the
modal
flag, so a user can't really do anything on your site while the dialog is open, BUT the UI keeps responsive.Again, there is actually no way to "hold" code execution. Any approach in that direction would freeze the UI thread since Javascript and UI updates share the same thread.
Since quite a few years, developers pushed Javascript to be more and more non-blocking (Javascript in browsers actually always followed that route, which is very good thing). The idea was reborn with nodeJS on the backend. So, you're swimming upstream here, don't do it.
Whatever the problem is you try to solve there, try to think in different way. Think functional, use callbacks and events, think... ECMAscript! :p