如何让代码等待确定/取消按钮选择?

发布于 2024-11-08 03:28:56 字数 1064 浏览 4 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

妄想挽回 2024-11-15 03:28:58

代码确实按顺序执行。 $("#dialog-confirm").dialog()的作用是弹出一个对话框。 alert(is_okay) 在上面的行执行完毕之前不会执行。但OkayCancel 是事件侦听器。

"Okay": function() {
    $(this).dialog("close");
}

上面的代码将事件侦听器分配给事件。这就是它所做的一切,它不执行这些函数,它只是将这些函数分配给事件调用。

我建议阅读一些有关事件和事件侦听器的内容。如果您打算认真使用 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. But Okay and Cancel are event listeners.

"Okay": function() {
    $(this).dialog("close");
}

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.

笑叹一世浮沉 2024-11-15 03:28:58
$("#dialog-confirm").dialog({  ... 
close :(event, ui) { alert(is_okay);}
})

或者您可以稍后绑定函数:

$( "#dialog-confirm" ).bind( "dialogclose", function(event, ui) {
   alert(is_okay);
});

代码不会像 alert 那样停止并继续,但仅在对话框关闭时才会显示消息。

$("#dialog-confirm").dialog({  ... 
close :(event, ui) { alert(is_okay);}
})

Or you can bind function later :

$( "#dialog-confirm" ).bind( "dialogclose", function(event, ui) {
   alert(is_okay);
});

The code doesn't stop and continue like it does with alert , but it will display message only when dialog is closed.

故事未完 2024-11-15 03:28:58

只需将打开对话框后的所有代码放入一个函数中,然后从对话框回调函数中调用该函数即可确定/取消。

function doTheRest(args) {
    alert(args);
}

// snippet from dialog options
"Okay": function() {
    $(this).dialog("close");
    doTheRest(1);
},

"Cancel": function() {
    $(this).dialog("close");
    doTheRest(0);
}

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.

function doTheRest(args) {
    alert(args);
}

// snippet from dialog options
"Okay": function() {
    $(this).dialog("close");
    doTheRest(1);
},

"Cancel": function() {
    $(this).dialog("close");
    doTheRest(0);
}
风流物 2024-11-15 03:28:58

浏览器使用基于事件的异步编程模型,其中许多事情可以(并且确实)同时发生。这就是样式转换(动画如滚动或淡入淡出)的工作原理。

您的示例显示一个对话框,然后引发警报。它不能“等待”用户单击按钮,因为这样做会阻止浏览器执行其他任何操作。

因此,您需要重构代码,以便在单击与操作关联的回调中的“确定”或“取消”按钮时执行您需要执行的操作。

换句话说,您需要:

buttons: {
  "Okay": function() { // this function is called when a user clicks the Okay button
    // do whatever work is required here
  }
}

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:

buttons: {
  "Okay": function() { // this function is called when a user clicks the Okay button
    // do whatever work is required here
  }
}
地狱即天堂 2024-11-15 03:28:58

JavaScript 中没有直接的方法来“等待”某些事情——异步事件通常通过回调来处理。这意味着您需要根据事件而不是顺序代码来考虑您的程序。而不是您当前的代码:

$("#dialog-confirm").dialog({
  ...
});

alert(is_okay);
// rest of code

您需要将“其余代码”部分包装到其自己的函数中,然后从对话框的“确定/取消”回调中调用该函数:

$("#dialog-confirm").dialog({
    ...

    buttons: {
    "Okay": function() {
        $(this).dialog("close");
        what_happens_if_okay();
    },

    Cancel: function() {
        $(this).dialog("close");
        what_happens_if_not_okay();
    }
    } // buttons

}); // dialog

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:

$("#dialog-confirm").dialog({
  ...
});

alert(is_okay);
// rest of 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:

$("#dialog-confirm").dialog({
    ...

    buttons: {
    "Okay": function() {
        $(this).dialog("close");
        what_happens_if_okay();
    },

    Cancel: function() {
        $(this).dialog("close");
        what_happens_if_not_okay();
    }
    } // buttons

}); // dialog
許願樹丅啲祈禱 2024-11-15 03:28:58

因此,如果我理解正确的话,您希望显示一个带有“确定”和“取消”选项的警报,并且除非单击“确定”,否则不会出现对话框

与其使用另一个警报,为什么不尝试使用另一个包含“确定”和“取消”的对话框呢?

在您的 html 中执行此操作:

<div id = "hiddenDialogElements">
  <button id = "Ok" onclick = "confirm(true)">OK</button>
  <button id = "Cancel" onclick = "confirm(false)">Cancel</button>
</div>

使用此 css:

#hiddenDialogElements
{
   display: none;
}

然后您可以在将创建 dialog 的事件上执行此操作(您想要等待的位置):

$('#hiddenDialogElements').dialog ({
//代码
});

这是:

function confirm(ifOk)
{
    if(ifOk)
    {
        //Create Dialog
    }else {
        //Do nothing
    }
}

So if I understand correctly you want an alert to show up with the options OK and Cancel and the Dialog would not come up unless OK was hit.

Instead of using another alert why not try using another dialog with Ok and Cancel in it?

In your html do this:

<div id = "hiddenDialogElements">
  <button id = "Ok" onclick = "confirm(true)">OK</button>
  <button id = "Cancel" onclick = "confirm(false)">Cancel</button>
</div>

With this css:

#hiddenDialogElements
{
   display: none;
}

Then you can do this on the event that will create the dialog (Where you want a wait):

$('#hiddenDialogElements').dialog({
//Code
});

And this:

function confirm(ifOk)
{
    if(ifOk)
    {
        //Create Dialog
    }else {
        //Do nothing
    }
}
哭了丶谁疼 2024-11-15 03:28:58

做这样的事情...

function MyAlert(Message, Title,FuncExecAfterOkPressed) {

$("<div>" + Message + "</div>").dialog({
    title: Title,
    dialogClass: "alert",
    width: "auto",
    modal:true,
    open: function (event, ui) { $(".ui-dialog-titlebar-close").hide(); },
    buttons: [
    {
        text: "Ok", click: function () {               
            $(this).dialog("close");
            if (FuncExecAfterOkPressed != null) {
                $.Callbacks().add(FuncExecAfterOkPressed).fire(null);
            }
        }
    }]
});

}

现在这样称呼它

 MyAlert(data, "Duplicate", function () {
                            alert("I m moumit");
                            $("#Name").val("");
                        });

你需要的一件事是 JQueryUI 插件...享受吧。

Do Something Like this ...

function MyAlert(Message, Title,FuncExecAfterOkPressed) {

$("<div>" + Message + "</div>").dialog({
    title: Title,
    dialogClass: "alert",
    width: "auto",
    modal:true,
    open: function (event, ui) { $(".ui-dialog-titlebar-close").hide(); },
    buttons: [
    {
        text: "Ok", click: function () {               
            $(this).dialog("close");
            if (FuncExecAfterOkPressed != null) {
                $.Callbacks().add(FuncExecAfterOkPressed).fire(null);
            }
        }
    }]
});

}

Now Call it like this

 MyAlert(data, "Duplicate", function () {
                            alert("I m moumit");
                            $("#Name").val("");
                        });

One thing u need is JQueryUI plugin... Enjoy.

还如梦归 2024-11-15 03:28:57

您可以将附加代码放入“确定”和“取消”按钮功能中。例如:

"Okay": function() {
    $(this).dialog("close");
    alert(is_okay);
},

You can put your additional code in the "Okay" and "Cancel" button functions. For example:

"Okay": function() {
    $(this).dialog("close");
    alert(is_okay);
},
淡淡離愁欲言轉身 2024-11-15 03:28:57

这不可能以“好的”方式完成,而且我强烈建议不要这样做。

您所描述的是一个完全阻塞的模态窗口/对话框,这对于 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文