【问题标题】:jQuery: Close Dialog after AJAX submitjQuery:AJAX 提交后关闭对话框
【发布时间】:2014-08-15 20:59:47
【问题描述】:

通过 AJAX 提交后,我想关闭表单。我查了谷歌和一些老话题,但我找不到任何东西!我有一个功能我 javascript 来显示我的表单,我还有另一个功能来 ajax 请求,我想我应该获取打开模式窗口的实例然后关闭它,但我不确定。

这是显示表单窗口的代码

$(function (){
function handler() {
if (this.readyState == this.DONE) {
    if (this.status == 200 && this.responseText != null) {
        $("#modal").html(this.responseText);
        return;
    }
    console.log('Something went wrong! Status = ' + this.status);
 }
}

var client = new XMLHttpRequest();
client.onreadystatechange = handler;

$(document).ready(function () {
   $(document).on('click','.showModal',function(event) {
    client.open("GET", $(this).attr("href"),true);
    client.send();
var dialog;
dialog =   $("#modal").dialog({
        autoOpen:true,
        width: 400,
        height: 450,
        modal: true,
        /*close: function () {
            $("#modal").html('');
        }*/
        buttons: [{ 
           text: "Cancelar", 
           click: function() { 
             dialog.dialog("close"); 
           }
        }],
        close: function () {
            $("#modal").html('');
        }

    });
    return false;
 });

});
});    

这是我通过 AJAX 发送数据并尝试关闭窗口的代码:

$(function(){
$("#y").on('click',function(event){
     event.preventDefault();
     var id = $("input[name=id]").val();
     var action = 'del';
     $.post("album/ajax",{
          action : action,
          id     : id
        },
        function(data){
          if(data.response == true){
            if(action == 'del') {
              var table = $('#table');
              table.find('tr').each(function(indice){
              $(this).find('td').each(function(indice){
                if($(this).text() == ''){
                var valor = $(this).find('input[name=cod]').val();
                 if(valor == id){
                    $(this).closest('tr').remove();
                    /*var dialog;
                    dialog =   $("#modal").dialog();
                    dialog.closest(".ui-dialog-content").dialog("close");
                    dialog.dialog("close");*/
                  }
                }
             });
            });
           }
         } else {
               alert("Nao foi possivel deletar!");

         }
        },'json');

 });
});

【问题讨论】:

标签: javascript jquery ajax forms jquery-ui


【解决方案1】:

试试 $("#modal").dialog("close");

【讨论】:

  • 我做到了,我收到了这个错误消息错误:无法在初始化之前调用对话框上的方法;试图调用方法“关闭”
  • 我不太了解您的代码。这一切都发生在一页上吗?所以你打电话给 $("#modal").dialog("close");紧随 $(this).closest('tr').remove(); ?
  • 我正在测试,但是是的,它发生在我所有的模态对话框中,我只是将我的 var 对话框声明为全局,它已解决但我不确定是否是最好的方法!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多