如何重现“等待”? JavaScript的confirm()函数提供的功能? [英] How can I reproduce the "wait" functionality provided by JavaScript's confirm() function?

查看:79
本文介绍了如何重现“等待”? JavaScript的confirm()函数提供的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个自定义的窗口内确认对话框(基于jQuery UI),我们可以使用它来控制窗口的所有方面(按钮文本等)。我真正喜欢的是控件的行为与内置的confirm()函数的行为完全相同(调用confirm(),脚本执行暂停直到用户单击ok或取消,confirm()返回true或false) 。我已经四处寻找解决方案,我收到的关闭看起来像这样:

I'm trying to write a custom in-window confirm dialog (based on jQuery UI) that we can use to control all aspects of the window (button text, etc). What I'd really like is for the control to behave exactly the way that the built-in confirm() function behaves (call confirm(), script execution pauses until the user clicks ok or cancel, confirm() returns true or false). I have looked around to find solutions for this, and the closes that I have come looks something like this:

//title = text to be displayed for title
//text = text to be displayed in body of dialog
//func = name of function to be called upon user selection, result will be passed as first parameter
function aw_confirm(title, text, func){
    <%= ClientID %>_retVal = null;
    <%= ClientID %>_func = func;
    <%= ClientID %>_show(title, text);
    <%= ClientID %>_waitForSelection(func);
}

function <%= ClientID %>_waitForSelection(func){
    if (<%= ClientID %>_retVal != null){
        var r = <%= ClientID %>_retVal;
        <%= ClientID %>_retVal = null;
        func(r);
    }
    else setTimeout("<%= ClientID %>_waitForSelection(<%= ClientID %>_func)", 10);
}

这是一个aspx控件,因此<%= ClientID%> _在所有事情之前,为了确保没有人用其他控件中的代码覆盖函数。

This is an aspx control, thus the <%= ClientID %>_ before everything in order to make sure that nobody overwrites the functions with code in other controls.

正如你所看到的,这个当前的设置要求程序员编写一个处理程序(这里当用户进行选择时调用func)。我真正喜欢的是aw_confirm()等到用户做出选择然后返回。有没有办法做到这一点?提前感谢您提供的任何帮助或建议!

As you can see, this current setup requires the programmer to write a handler (here called func) to be called when the user makes a selection. What I'd really like is for aw_confirm() to just wait until the user makes a selection and then return. Is there a way to do this? Thanks in advance for any help or advice you can provide!

推荐答案

Javascript主要基于事件。代码对事件做出反应(点击按钮,计时器等)。 JQuery UI也遵循这个范例。你正在尝试的是反对这一点,因此即使可能也会很难。您应该遵循基于事件的方法。

Javascript is mostly event-based. The code reacts to events (a clicked button, a timer etc). JQuery UI also follows this paradigm. What you are trying is working against this and will be therefore quite hard if even possible. You should follow the event based approach.

它与使用确认的代码没有太大区别。如果你有这个:

It doesn't have to be much different from the code which uses confirm. If you had this:

ret = confirm("Hello User")
// do something

你现在可以写了

aw_confirm("Hello user", "", function(ret){
  // do something
})

使用匿名函数作为回调。

using an anonymous function as callback.

这篇关于如何重现“等待”? JavaScript的confirm()函数提供的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆