【问题标题】:Don't return results until jQuery dialogbox closes?在 jQuery 对话框关闭之前不返回结果?
【发布时间】:2015-05-06 21:10:38
【问题描述】:

我正在尝试根据 jQuery 对话框选择返回结果。在发送退货声明之前,我一直保持警报消息。如何保持结果返回,直到我在对话框中做某事?值将是“真”或“假”

        function ConfirmDone() {
            var results;
            $('<div></div>').appendTo('body')
               .html('<div><h6>Are you sure you want to lose unsaved changes?</h6></div>')
               .dialog({
                   modal: true, title: 'Delete message', zIndex: 10000, autoOpen: true,
                   width: 'auto', resizable: false,
                   buttons: {
                       Yes: function () {
                           $(this).dialog("close");
                           results=true;
                       },
                       No: function () {
                           $(this).dialog("close");
                           results=false;
                       }
                   },
                   close: function (event, ui) {
                       $(this).remove();
                       results = false;
                   }
               });
            alert(results); //This is calling same when dialog shows up
            return results;

        }

这里有什么问题?

更新:

我不确定。回调函数可以应用于我的代码吗?正如@Barmar 在重复帖子中提到的那样

更新

@Ajax.ActionLink(
    new { CommunicationLocation = commemail.Location,  CommunicationType = "email" },
            new AjaxOptions()
             {
                 HttpMethod = "Post",
                 UpdateTargetId = "DivEmailContainer",
                 InsertionMode = InsertionMode.Replace,
                 OnBegin = "return ConfirmDone(function(success) {alert('You said: ' + (success ? 'Yes' : 'No'))});"
               },
               new { @class = "linkbutton" })
               }

     $(document).ready(function () {
                $("#deletedialog").dialog({
                    autoOpen: false,
                    modal: true
                });
            });
            function ConfirmDone(callback) {
                 $("#deletedialog").dialog({
                     buttons: {
                         "Delete":{ text: "Delete",
                                    class: "btn btn-success",
                                    click: function () {
                                        $(this).dialog("close");
                                        callback(true);
                                    }
                         },
                         "Cancel": {
                             text: "Cancel",
                             class: "linkbutton",
                             click: function () {
                                $(this).dialog("close");
                                callback(false);
                            }
                        }
                     }
                 });


         $("#deletedialog").dialog("open");

【问题讨论】:

  • @Barmar,我不明白重复的帖子会适用于我的代码吗?我很困惑。
  • 对不起,复制的选择很糟糕,因为它使用了不同的插件。但总体思路是对话是异步的。你的ComfirmDone 函数应该接受一个回调参数,并在close: 函数中调用它。
  • 有人帮忙,还有其他选择吗?
  • 也许此搜索中的一个问题会有所帮助:stackoverflow.com/…

标签: javascript jquery asp.net-mvc asp.net-mvc-4 jquery-ui


【解决方案1】:

对话框是异步的。你需要传递一个回调函数:

    function ConfirmDone(callback) {
        var results;
        $('<div></div>').appendTo('body')
           .html('<div><h6>Are you sure you want to lose unsaved changes?</h6></div>')
           .dialog({
               modal: true, title: 'Delete message', zIndex: 10000, autoOpen: true,
               width: 'auto', resizable: false,
               buttons: {
                   Yes: function () {
                       $(this).dialog("close");
                       callback(true);
                   },
                   No: function () {
                       $(this).dialog("close");
                       callback(false);
                   }
               },
               close: function (event, ui) {
                   $(this).remove();
                   callback(false);
               }
           });
    }

你可以这样做:

ConfirmDone(function(success) {
    alert("You said: " + (success ? "Yes" : "No"));
});

【讨论】:

  • 我需要从ConfirmDone(function(success) { }方法返回值吗?
  • 不,你没有。回调是异步运行的,它不会等待用户响应对话框。您不能等待 Javascript 中的用户交互,您只需提供在用户执行某项操作时运行的函数。
  • 但是我这样调用`new AjaxOptions() { ... OnBegin = "return ConfirmDone()" },
  • 你不能那样做。一切都必须使用回调来完成。
  • 我的OnBegin 期待结果。请检查更新的帖子。我喜欢这样,但不起作用。
猜你喜欢
  • 2011-02-15
  • 1970-01-01
  • 1970-01-01
  • 2011-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-24
  • 2018-02-09
相关资源
最近更新 更多