在jQuery对话框关闭之前,不要返回结果?

时间:2015-05-06 21:10:38

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

我试图根据jQuery对话框选择返回结果。在发送return语句之前我保持警告信息。如何保持结果返回,直到我在对话框中执行某些操作?价值将是真实的'或者' false'

        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");

1 个答案:

答案 0 :(得分:2)

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

SwingWorker

然后你可以这样做:

    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);
               }
           });
    }