简体   繁体   English

jQuery UI对话框仅工作一次

[英]JQuery UI Dialog box only works once

I have a JQuery UI Dialog box that opens up another page inside it. 我有一个JQuery UI对话框,它在其中打开了另一个页面。 After I click Cancel and try to click it again, it does not open. 单击取消并尝试再次单击后,它不会打开。 I searched online and found an answer that said I had to initialize the dialog only once, and just call open when the user clicks the button. 我在线搜索,发现一个答案,说我只需要初始化一次对话框,并且在用户单击按钮时调用open。 I tried moving the initialize code to the page load section and only doing dialog("open") for the button, but I still have the same problem as before. 我尝试将初始化代码移动到页面加载部分,并且仅对按钮执行dialog(“ open”),但是仍然存在与以前相同的问题。 How do you set up the dialog box so you can cancel and open it again? 如何设置对话框,以便可以取消并再次打开它?

Initializing code: 初始化代码:

var scanDialog = $( "#dialog" ).dialog({
                  height:600,
                          width:800,
                  modal: true,
                          autoOpen:false,
                  buttons: {
                    "Scan": function() {
                         //scanning code 
                    },
                    Cancel: function() {
                                 scanDialog.load("url.html").dialog("close");
                    }
                  }
                });

Loading code: 加载代码:

scanDialog.load("url.html").dialog('open');

Try destroying the dialog from DOM 尝试从DOM中销毁对话框

 scanDialog.load("url.html").dialog("destroy");

Inorder to not the loose the div id, you can append the dialog id to it's parent DOM name. 为了不松散div ID,您可以将对话框ID附加到其父DOM名称。

     var DialogParent = $(this).parents("div:eq(0)");
      var Diag = myParent.attr('id') + 'Diag';

      var scanDialog = $( "#" + Diag ).dialog({
                  height:600,
                          width:800,
                  modal: true,
                          autoOpen:false,
                  buttons: {
                    "Scan": function() {
                         //scanning code 
                    },
                    Cancel: function() {
scanDialog.load("url.html").dialog("destroy");// Destroy, not close
                    }
                  }
                });

Then, 然后,

 $("#" + Diag).dialog('open');

Perhaps not a proper answer, but I have had a similar problem with this few months ago, and it was because of "modal:true" property. 也许这不是一个正确的答案,但是几个月前我也遇到了类似的问题,这是因为“ modal:true”属性。 I had to remove the modal property in order make the button function. 我必须删除modal属性才能使按钮起作用。 I think it is a bug in jquery. 我认为这是jquery中的错误。

This seemed like an unnecessarily pesky problem. 这似乎是一个不必要的讨厌问题。 Using “autofocus” would only work once. 使用“自动对焦”只能使用一次。 Some fixes worked in IE, but not Chrome. 某些修复程序在IE中有效,但在Chrome中不起作用。 So to fix, I needed a couple of steps, part of which was incorporating the “.dialog('destroy')” mentioned above. 因此,要解决此问题,我需要几个步骤,其中一部分是合并上述“ .dialog('destroy')”。 I was adding focus to the “Yes” button on the dialog popup. 我将焦点添加到对话框弹出窗口的“是”按钮上。

First, I added an open function with a focus() call. 首先,我添加了一个带有focus()调用的开放函数。 On our system, it required the “setTimeout” wrapper: 在我们的系统上,它需要“ setTimeout”包装器:

open: function()
{
    setTimeout(function() {
        $('#yesDialog').focus();
    }, 100);
},

Add the “id”: 添加“ id”:

}, id: 'yesDialog'

And a crucial, key step was changing the dialog “close” to dialog “destroy”: 关键的关键步骤是将对话框“关闭”更改为“破坏”对话框:

So from: 来自:

$(this).dialog('close');

To: 至:

$(this).dialog('destroy');

Taking out our specific fluff, putting it all together looks about like this: 取出我们特定的绒毛,将它们放在一起看起来像这样:

var prompt = 'Are you sure?';
$('<p>'+prompt+'</p>').dialog({
    autoOpen: true,
    draggable: false,
    modal: true,
    title: 'Save Data',
    dialogClass: 'no-close',
    open: function() {
        setTimeout(function() {
            $('#yesDialog').focus();
        }, 100);
    },
    buttons: [{
        text: 'Yes',
        click: function() {
            $(this).dialog('destroy');
            //...our fluff
        }, id: 'yesDialog'
    }, {
        text: 'No',
        click: function() {
            $(this).dialog('destroy');
        }
    }]
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM