<i id='4XEKD'><tr id='4XEKD'><dt id='4XEKD'><q id='4XEKD'><span id='4XEKD'><b id='4XEKD'><form id='4XEKD'><ins id='4XEKD'></ins><ul id='4XEKD'></ul><sub id='4XEKD'></sub></form><legend id='4XEKD'></legend><bdo id='4XEKD'><pre id='4XEKD'><center id='4XEKD'></center></pre></bdo></b><th id='4XEKD'></th></span></q></dt></tr></i><div id='4XEKD'><tfoot id='4XEKD'></tfoot><dl id='4XEKD'><fieldset id='4XEKD'></fieldset></dl></div>

<legend id='4XEKD'><style id='4XEKD'><dir id='4XEKD'><q id='4XEKD'></q></dir></style></legend>

<small id='4XEKD'></small><noframes id='4XEKD'>

      <tfoot id='4XEKD'></tfoot>
          <bdo id='4XEKD'></bdo><ul id='4XEKD'></ul>
      1. 将异步 jQuery Dialog 更改为同步?

        时间:2023-09-04
      2. <tfoot id='f9S45'></tfoot>

          <tbody id='f9S45'></tbody>

            <legend id='f9S45'><style id='f9S45'><dir id='f9S45'><q id='f9S45'></q></dir></style></legend>

                <bdo id='f9S45'></bdo><ul id='f9S45'></ul>
                <i id='f9S45'><tr id='f9S45'><dt id='f9S45'><q id='f9S45'><span id='f9S45'><b id='f9S45'><form id='f9S45'><ins id='f9S45'></ins><ul id='f9S45'></ul><sub id='f9S45'></sub></form><legend id='f9S45'></legend><bdo id='f9S45'><pre id='f9S45'><center id='f9S45'></center></pre></bdo></b><th id='f9S45'></th></span></q></dt></tr></i><div id='f9S45'><tfoot id='f9S45'></tfoot><dl id='f9S45'><fieldset id='f9S45'></fieldset></dl></div>

                1. <small id='f9S45'></small><noframes id='f9S45'>

                2. 本文介绍了将异步 jQuery Dialog 更改为同步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  目前,我正在努力用 jquery 对话框替换alert"/confirm".

                  Currently, I'm working to replace "alert'/"confirm" with the jquery dialog.

                  但是大多数遗留代码都是以某种异步方式编写的,这使得更改变得困难.有什么方法可以使 jquery 对话框以同步方式工作?(不要使用循环或回调函数)

                     For example:
                     function run()
                     { 
                        var result = confirm("yes or no");
                        alert( result );
                        \more codes here
                     }
                  

                  在此示例中,警报和其他代码将在用户选择后执行.

                  In this example the alert and other codes will be executed after user's choice.

                  如果我们使用 jquery 对话框var 结果 = $dialog.open()它将继续执行警报,这是异步的.

                  If we use jquery dialog var result = $dialog.open() It will continue to execute the alert, which is asynchronous.

                  目前,我的解决方案是在 OK|Cancel 函数中使用回调函数.例如:

                  Currently, my solution is to use call back function in the OK|Cancel function. For example:

                      OK: function ()
                     {
                         $dialog.close();
                         alert("yes");
                         //more codes here
                      }
                  

                  这种方法可行,但是很难让所有同步的代码都变成异步的,需要做很多改动(见下例).所以我在找同步的jQuery Dialog,可以吗??

                  This method works but it is difficult to make all the synchronous codes become asynchronous, which requires a lot of change (see the following example). So I'm looking for the synchronous jQuery Dialog, is it possible??

                  例如:(实际代码比下面的例子复杂得多)

                  For example: ( The real codes are much more complicated than the following example)

                       function f1()
                      {
                            if ( confirm("hello") )    f2();
                            alert("no");
                      } 
                  
                      function f2()
                      {
                            if( confirm("world") )    f3();
                            alert("no");
                      }
                  
                      function f3()
                      {
                            return confirm("!") ;
                      }
                  

                  <小时>

                  另一个例子:


                  Another example:

                  vendorObject.on('some-event', function() {
                      if(confirm("Do you really want to do that?")) {
                          return true;
                      }
                      else {
                          return false; // cancel the event
                      }
                  });
                  

                  ...这里供应商对象触发一个事件,如果用户确认,则必须取消该事件.只有当事件处理程序返回 false - 同步时,才能取消事件.

                  ... here the vendor object fires an event, which has to be cancelled if the user confirms. The event can only be cancelled if the event handler returns false - synchronously.

                  推荐答案

                  简短的回答是不,你将无法保持你的代码同步.原因如下:

                  The short answer is no, you won't be able to keep your code synchronous. Here's why:

                  • 为了使其同步,当前执行的脚本必须等待用户提供输入,然后继续.
                  • 虽然当前正在执行脚本,但用户无法与 UI 交互.事实上,直到脚本执行完毕后,用户界面才会更新.
                  • 如果脚本在用户提供输入之前无法继续,并且用户在脚本完成之前无法提供输入,那么您最接近的就是浏览器挂起.

                  为说明此行为,请调试您的代码并在更改 UI 的行之后的行上设置断点:

                  To illustrate this behavior, debug your code and set a break point on the line following a line that changes the UI:

                  $("body").css("backgroundColor", "red");
                  var x = 1;  // break on this line
                  

                  请注意,您的页面背景还不是红色.在您恢复执行并且脚本完成执行之前,它不会变为红色.当您使用调试器暂停脚本执行时,您也无法单击页面中的任何链接.

                  Notice that your page background is not yet red. It won't change to red until you resume execution and the script finishes executing. You are also unable to click any links in your page while you've got script execution paused with your debugger.

                  alert()confirm() 有一个例外.这些是浏览器控件,其处理方式与实际网页 UI 元素不同.

                  There is an exception to this rule for alert() and confirm(). These are browser controls, and are treated differently than actual web page UI elements.

                  好消息是转换您的代码真的应该不是很难.大概,您的代码目前看起来像这样:

                  The good news is that it really shouldn't be very hard to convert your code. Presumably, your code currently looks something like this:

                  if (confirm("continue?")) {
                      // several lines of code if yes
                  }
                  else {
                      // several lines of code if no
                  }
                  // several lines of code finally 
                  

                  您的异步版本可以创建一个函数 ifConfirm(text, yesFn, noFn, finallyFn) 并且您的代码看起来非常相似:

                  Your asynchronous version could create a function ifConfirm(text, yesFn, noFn, finallyFn) and your code would look very much the same:

                  ifConfirm("continue?", function () {
                      // several lines of code if yes
                  },
                  function () {
                      // several lines of code if no
                  },
                  function () {
                      // several lines of code finally 
                  });
                  

                  为了回应您添加到问题中的其他示例,不幸的是,该代码需要重构.不可能有同步的自定义确认对话框.要在事件需要继续或取消的情况下使用自定义确认对话框,您只需始终取消事件并在 yesFn 回调中模拟事件.

                  In response to the additional example you added to your question, unfortunately that code will need to be refactored. It is simply not possible to have synchronous custom confirmation dialogs. To use a custom confirmation dialog in the scenario where an event needs to either continue or cancel, you'll just have to always cancel the event and mimic the event in the yesFn callback.

                  例如一个链接:

                  $("a[href]").click(function (e) {
                      e.preventDefault();
                      var link = this.href;
                      ifConfirm("Are you sure you want to leave this awesome page?", function () {
                          location.href = link;
                      });
                  });
                  

                  或者,一种形式:

                  $("form").submit(function (e) {
                      e.preventDefault();
                      var form = this;
                      ifConfirm("Are you sure you're ready to submit this form?", function () {
                          form.submit();
                      });
                  });
                  

                  这篇关于将异步 jQuery Dialog 更改为同步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:我将如何实现 stackoverflow 的悬停对话框? 下一篇:jQuery UI Dialog 个人 CSS 样式

                  相关文章

                  最新文章

                  <legend id='McVFh'><style id='McVFh'><dir id='McVFh'><q id='McVFh'></q></dir></style></legend>

                      • <bdo id='McVFh'></bdo><ul id='McVFh'></ul>

                      <i id='McVFh'><tr id='McVFh'><dt id='McVFh'><q id='McVFh'><span id='McVFh'><b id='McVFh'><form id='McVFh'><ins id='McVFh'></ins><ul id='McVFh'></ul><sub id='McVFh'></sub></form><legend id='McVFh'></legend><bdo id='McVFh'><pre id='McVFh'><center id='McVFh'></center></pre></bdo></b><th id='McVFh'></th></span></q></dt></tr></i><div id='McVFh'><tfoot id='McVFh'></tfoot><dl id='McVFh'><fieldset id='McVFh'></fieldset></dl></div>

                    1. <small id='McVFh'></small><noframes id='McVFh'>

                    2. <tfoot id='McVFh'></tfoot>