如何重现JavaScript的confirm()函数提供的“等待”function?

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

//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){ _retVal = null; _func = func; _show(title, text); _waitForSelection(func); } function _waitForSelection(func){ if (_retVal != null){ var r = _retVal; _retVal = null; func(r); } else setTimeout("_waitForSelection(_func)", 10); } 

这是一个aspx控件,因此 _在所有内容之前,以确保没有人用其他控件中的代码覆盖函数。

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

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

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

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

你现在可以写了

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

使用匿名函数作为回调。