javascript 使用 jQuery Dialog 时如何停止脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17078100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:01:01  来源:igfitidea点击:

How to stop script when using jQuery Dialog?

javascriptjqueryjquery-ui-dialog

提问by Jude Duran

I'm sure this has been asked here many times. The latestone I saw was on Dec. 2012. But I would like to ask if someone's got a fix/better workaroundfor this problem.

我相信这已经在这里被问过很多次了。在最新的一个我看到的是在2012年12月。但我想问一下是否有人对此问题有修复/更好的解决方法

I'm using jQueryUI Dialogand I would like to ask if it is possible to stop the script executionwhen the dialog pops up and the user haven't selected an answer yet. Just like the javascript confirm()does.

我正在使用jQueryUI Dialog,我想问一下,当弹出对话框并且用户尚未选择答案时,是否可以停止脚本执行。就像 javascriptconfirm()一样。

Here's what I have so far:

这是我到目前为止所拥有的:

jQuery

jQuery

$("#GlobalDialogBox").dialog({
        modal: true,
        buttons: {
        "Yes": function () { 
            callbackFunctionTrue();
            $(this).dialog('close'); 
         },
        "No": function () { 
            callbackFunctionFalse();
            $(this).dialog('close'); 
         }
      }
    });

   alert("This should fire after answering the dialog box."); //but this fires as soon as the dialog pops up.. :(

The alertis a common part of the script that fires after answering the dialog box regardless of the answer. On my fix, I just inserted the alertin callbackFunctionTrue()and callbackFunctionFlase()and it does the job.

alert是脚本的常见部分,无论答案如何,都会在回答对话框后触发。在我的修正,我刚插入的alertcallbackFunctionTrue()callbackFunctionFlase()和它的工作。

But what if I don't want to do that? How would I stop the script when the dialog pops up so that I can freely add my codes below the dialogscript? Does anybody have a better solution? Any help would be appreciated! Thanks in advance! :]

但是如果我不想那样做呢?当对话框弹出时我将如何停止脚本,以便我可以在dialog脚本下自由添加我的代码?有人有更好的解决方案吗?任何帮助,将不胜感激!提前致谢!:]

采纳答案by lukaleli

When dialog is being closed it triggers close event. You can do that by adding event listener to your dialog:

当对话框被关闭时,它会触发关闭事件。您可以通过向对话框添加事件侦听器来做到这一点:

$("#GlobalDialogBox").on( "dialogclose", function( event, ui ) {
      alert("something");
});

or in more consistent way:

或更一致的方式:

$("#GlobalDialogBox").dialog({
    modal: true,
    buttons: {
        "Yes": function () { 
            callbackFunctionTrue();
            $(this).dialog('close'); 
         },
        "No": function () { 
            callbackFunctionFalse();
            $(this).dialog('close'); 
         }
    },
    close: function(event, ui){
       alert("something");
    }
});

回答by bitwiser

You can't really pause script execution arbitrarily. Callback functions are your best solution in this case, so below your buttons options on the dialog add a close event handler and put your alert and any other code you want to execute after the dialog closes in there.

你真的不能随意暂停脚本执行。在这种情况下,回调函数是您最好的解决方案,因此在对话框上的按钮选项下方添加一个关闭事件处理程序,并将您的警报和您想要在对话框关闭后执行的任何其他代码放入其中。

回答by Ramesh

function dialog_confirm(){
  $( "#dialog-confirm" ).dialog({
    //autoOpen: false,
    resizable: false,
    height:140,
    modal: true,
    buttons: {
      "Delete all items": function() {
        $( this ).dialog( "close" );
        dialog_confirm_callback(true);
      },
      Cancel: function() {
        $( this ).dialog( "close" );
        dialog_confirm_callback(false);
      }
    }
  });
}
function dialog_confirm_callback(value) {
  if (value) {
    alert("Confirmed");
  } else {
    alert("Rejected");
  }
}

回答by adrianz

Not sure why you don't want to do that. What you have implemented is the same as I implemented when in the same situation and it worked just fine. Launch the dialog in its own JavaScript function, and launch your yes/no functions based on which element gets clicked. No alert box required.

不知道为什么你不想这样做。您实施的与我在相同情况下实施的相同,并且效果很好。在它自己的 JavaScript 函数中启动对话框,并根据点击的元素启动是/否函数。不需要警告框。

回答by Ziv

you can use the returnto stop the function.

您可以使用返回来停止该功能。