Javascript 引导确认模式中的等待值?

Javascript 引导确认模式中的等待值?,javascript,jquery,twitter-bootstrap,Javascript,Jquery,Twitter Bootstrap,这个问题看起来很愚蠢,但我不知道如何等待确认按钮返回值。Javascript基于单线程,因此我需要知道如何设置回调函数,直到按下按钮,这是对话框的结构: <div class="modal fade" id="confirmation" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class=

这个问题看起来很愚蠢,但我不知道如何等待确认按钮返回值。Javascript基于单线程,因此我需要知道如何设置回调函数,直到按下按钮,这是对话框的结构:

<div class="modal fade" id="confirmation" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">Confirm</h4>
            </div>
            <div class="modal-body">
                <label>Are you sure?</label>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default btn-ok" data-dismiss="modal">Continue</button>
                <a class="btn btn-danger" data-dismiss="modal">Cancel
                </a>
            </div>
        </div>
    </div>
</div>

<button id="go">press me</button>
实际上,当用户按下按钮时,会出现一个引导对话框,只有当用户按下“继续”按钮
.bnt ok
,我才需要显示一个警报,但实际上我不知道如何执行此操作。有人能告诉我怎么做吗?谢谢

更新

1用户触发事件并执行功能
one

one: function()
{
    if(two()) //if function two return true then show alert
    {
        alert(true);
    }
}
2函数2显示引导对话框:

 two: function()
 {
    $('#confirmation').show();

   //here I need to check if the user has pressed btn-ok, if yes,
   //I need to return true, otherwise false

   return true;
 }

3功能
two
出现问题,因为在用户按钮单击之前,无法停止该功能。因此,将显示确认对话框,但函数返回true。如何等待用户单击?

只需将单击处理程序附加到
.btn ok
元素

因此:

$('.btn-ok').click(function(){
   alert(true);
});

编辑:

查看使用ES6承诺解决问题的方法

const modal = new Promise(function(resolve, reject){
       $('#confirmation').modal('show');
       $('#confirmation .btn-ok').click(function(){
           resolve("user clicked");
       });
       $('#confirmation .btn-danger').click(function(){
           reject("user clicked cancel");
       });
      }).then(function(val){
        //val is your returned value. argument called with resolve.
        alert(val);
      }).catch(function(err){
        //user clicked cancel
        console.log("user clicked cancel", err)
      });

。注意,它正在使用,您应该检查浏览器支持或传输代码(例如使用babel)

我很理解这个问题。 纯类型脚本解决方案如下所示:

const result: boolean = confirm(message);
                if (result == true) {
                    return ConfirmationResult.ConfirmationResultPositive;
                } else {
                    return ConfirmationResult.ConfirmationResultNegative;
                }
其中ConfirmationResult.ConfirmationResultPositive是一些枚举。 交易确认功能显示标准js窗口,等待用户结果(阻止进一步执行)。不幸的是,引导没有这个标准功能。例如,WinForms、WPF和UMP(clear async)具有相同的标准行为。
我还认为,有了Promise,我们可以用引导模式解决它。

问题更复杂,您的解决方案是一个解决方案,因为想象一下,不是执行活动的按钮,而是一个显示引导对话框的函数,一切都是different@AgainMe不知道你的意思让我解释清楚,因此,我的引导对话框由一个按钮压力调用的函数显示。现在这个函数返回一个值,但是我需要在它返回一个值之前停止这个函数,因为当引导对话框显示时,我需要等待用户的选择,然后返回一个函数的值,该函数允许我用另一种方法执行任务。我不知道是不是更清楚,如果不是,我可以给你举个例子。@AgainMe javascript是异步语言,一切都基于事件和回调,有一些方法可以让它看起来像承诺一样同步。你愿意使用ES6吗?让我们来看看。
const result: boolean = confirm(message);
                if (result == true) {
                    return ConfirmationResult.ConfirmationResultPositive;
                } else {
                    return ConfirmationResult.ConfirmationResultNegative;
                }