如何在执行javascript中的进一步语句之前等待bootbox.prompt()结果

How to wait for the bootbox.prompt() result before executing the further statements in javascript?

本文关键字:bootbox 等待 prompt 结果 语句 执行 javascript 进一步      更新时间:2023-09-26

我有以下代码:

var bootboxresult;
bootbox.prompt('Please enter some data', function(result) {
if (result != null) {
bootboxresult = result;
}});
alert(bootboxresult);
if(bootboxresult === null)
{
return;
}
if(bootboxresult != null)
{
Some Code To Excecute
}

代码显示提示框,但变量"bootboxresult"的内容总是为空或未定义。

我需要进一步的代码应该等待bootbox.prompt()将值存储在"bootboxresult"变量中。

请建议。

我认为你可以在bootbox的回调中编写所有的代码。参见下面的示例。

        var popup_content = "<b>Are you sure?</b><br><br>Do you want to delete?";
        bootbox.confirm(popup_content, function(result) {
              if(result){
                  // Write all your code here.  
              }else{
                 // else part code goes here
              }
        });