【问题标题】:unable to close modal window after ajax success callajax成功调用后无法关闭模式窗口
【发布时间】:2016-04-03 21:45:06
【问题描述】:

我正在发出如下的 jquery ajax 请求

编辑

点击编辑按钮

$('.mymodalbtn').click(function($this){
            var id = $this.data('id'); 
            $('[name="id"]').val(id);
        });
    });

带有可编辑字段的模态窗口打开,在提交表单时我正在如下进行 ajax 调用

$('#mymodalForm').on('submit',function(e){
            e.preventDefault();
            var successFlag=false;

            $.ajax({
                url: "/student/"+selectedId ,
                data : {'id':selectedId},
                type: 'PUT',
                datatype: "json",
                success: function(data){
                    $.gritter.add({
                        title: "Student",
                        text: data,
                        time: '1000'
                    }),
                }
            });
        });

<!-- Nifty Modal HTML -->
    <div class="md-modal colored-header md-effect-9" id="mymodalWin">
        <div class="md-content">
            <div class="modal-header">
                <h3>Student</h3>
            </div>
            <form id="mymodalForm" method="post" action="">
            <div class="modal-body form">   
                <input type="text" value="2" name="id"/>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default btn-flat md-close" data-dismiss="modal">Cancel</button>
                <button type="submit" class="btn btn-primary btn-flat" id="edit-selected-transaction" data-dismiss="modal">Submit</button>
            </div>
            </form>
        </div>
    </div>
    <div class="md-overlay"></div>  

如果成功,我会尝试自动关闭模式窗口,否则会显示错误并停留在模式窗口上。

我尝试使用 .complete 但运气似乎没有问题!。

我也尝试过 .hide() ,但点击编辑按钮后,模式窗口没有出现。谁能告诉我如何自动关闭引导模式窗口。

【问题讨论】:

  • 你已经到了那里,重要的是不要紧张。深吸一口气,意识到你今天比昨天更进一步!你的 JS 需要在你的函数中清理一下,就像我在下面发布的一样,所以我现在要修复它并在 JSFiddle 中运行你的代码以完成修复。
  • 快速修复:是 ('#mymodal').removeClass("md-show") 并添加 style="perspective:1300px"。这对我有用。但我想知道为什么 .modal('hide') 不能按预期工作。

标签: jquery twitter-bootstrap gritter


【解决方案1】:

如果你想在成功回调中自动关闭模态窗口,那么就这样做

$.ajax({
 success:function(data){
   $('#mymodal').modal('hide');
   // Rest of your code.
  }
});

【讨论】:

    【解决方案2】:

    在你的 ajax 调用中,像这样使用它......

    $.ajax({
     success:function(data){
       $.gritter.add({
             title: "Student",
             text: responseHTML,
             time: '1000'
        },
     complete: function(){
                $('#mymodal').hide();
                //Here, you are executing your event loop, or in this example the api "hide" for the "#mymodal" id element.
        }
    }); // I forgot a bracket here, my apologies.
    

    【讨论】:

    • 也有可能,如果您没有将“gritter”传递到您的 ajax 调用函数中,则在 ajax 调用中它不能被引用。但我需要查看更多您的代码才能提供进一步的建议。此外,古老的格言“如果它没有坏......”
    • 我已经编辑了我的问题,如果需要更多详细信息,请告诉我。
    • .hide() 隐藏模态窗口留下背景色,当我再次单击时,模态窗口没有打开。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-08
    • 1970-01-01
    • 2018-06-29
    相关资源
    最近更新 更多