2016-09-21 81 views
-1

我已經以下代碼:如何在執行javascript中的其他語句之前等待bootbox.prompt()結果?

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」變量中。

請指教。

+0

的[我如何返回從一個異步調用的響應?(可能的複製http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an - 異步調用) – CBroe

+0

謝謝@CBroe,它幫助並清除了我的同步和異步調用概念。 –

回答

1

我想你可以在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 
       } 

     }); 
相關問題