让 JavaScript 函数等待 Jquery 对话框模态的结果

Having a JavaScript function wait on the results of a Jquery dialog modal

本文关键字:模态 结果 对话框 Jquery JavaScript 函数 等待      更新时间:2023-09-26

假设我有函数 1,它需要函数 2 的结果才能继续。

但是 function2 通过触发 Jquery 对话框窗口打开,并提示用户选择两个按钮中的一个来获取它需要的东西。

然后,选择两个按钮会导致 function2 完成其工作并将其传递给 function1。

如何将在对话框窗口中选择按钮时给出的值恢复为 function2。

请参阅下面的代码。

$('#choice-dialog').dialog({
   autoOpen: false,
   resizable: false,
   height:140,
   modal: true,
   buttons: {
      "Proceed": function() {
         $( this ).dialog( "close" );
         true //the value to be sent to function2;
      },
      Cancel: function() {
         $( this ).dialog( "close" );
         false //the value to be sent to function2;
      }
   }
});
function function1(){
   if(function2(variable)===true){
      return true
   }
}
var function2 = function(variable){
   if(idIsUsed){
      $("#choice-dialog").dialog("open");
      return **choice made by user via dialog**;
   }else{
      return true;
   }
}
你不能

这样做。最好重新构建代码,以便对话框回调调用另一个具有truefalse的函数:

$('#choice-dialog').dialog({
   autoOpen: false,
   resizable: false,
   height:140,
   modal: true,
   buttons: {
      "Proceed": function() {
         $( this ).dialog( "close" );
         otherFunction(true);
      },
      Cancel: function() {
         $( this ).dialog( "close" );
         otherFunction(false);
      }
   }
});
var function2 = function(variable){
   if(idIsUsed){
      $j("#choice-dialog").dialog("open"); 
   } else {
      otherFunction(true);
   }
}