javascript 如何不关闭 jQuery UI 的对话框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17566908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:48:35  来源:igfitidea点击:

How to do not close Dialog of jQuery UI?

javascriptjqueryhtmljquery-ui

提问by smooster

    var error = 1;
    $(document).on('click', '.ui-icon-closethick', function(event){
        if(error == 1){
           alert('error');
           event.preventDefault();
           event.stopPropagation();
           return false;
        } 
    })

How to do not close Dialog of jQuery UI? Now if i click on close button (x) then i have alert error, but dialog is closing.

如何不关闭 jQuery UI 的对话框?现在,如果我单击关闭按钮 (x),则会出现警报错误,但对话框正在关闭。

LIVE DEMO

现场演示

回答by anmarti

You can add the beforeCloseoption to your dialog and return false on it:

您可以将beforeClose选项添加到对话框中并在其上返回 false:

$("#dialog").dialog({
   beforeClose: function(){
     return false;
   }
});

Demo: http://jsfiddle.net/UfpHz/9/

演示:http: //jsfiddle.net/UfpHz/9/

回答by Dipesh Parmar

Well you can do this by removing close button.

那么你可以通过删除关闭按钮来做到这一点。

$("#YOUR_DIALOG_DOM_ID").dialog({
   closeOnEscape: false,
   open: function(event, ui)
   {
      $(".ui-dialog-titlebar-close", ui.dialog || ui).hide();
   }
});

回答by Nikko Reyes

You can use the beforeCloseevent to prevent the dialog from closing.

您可以使用该beforeClose事件来防止对话框关闭。

Like this:

像这样:

$( "#dialog" ).dialog({
    beforeClose: function(){
        if(error == 1){
            alert('error');
            return false;
        } 
    }
});

回答by Stan

You need to look for errors on beforeCloseevent and return trueor falsethere.

您需要在beforeClose事件中查找错误并返回truefalse在那里。

var error = 1;

$(function () {
    $("#dialog").dialog({
        beforeClose: function (event, ui) {
            if (error === 1) { // in javascript you compare with ===, not ==
                alert('error');
                return false; // error, dialog will not close
            }
            return true; // no error, dialog will close
        }
    });
});

http://jsfiddle.net/RHhwV/

http://jsfiddle.net/RHhwV/

回答by Chirag Vidani

You can handle close event also

您也可以处理关闭事件

$(function() {
      $( "#dialog" ).dialog({
          close: function(event,ui){
              $(this).dialog('open');
          }
      });
  });

more documentation can be found at this link

可以在此链接中找到更多文档

Demo

演示

回答by Rachit Doshi

Use

利用

beforeClose: function( event, ui ) {return false;}

from url : http://api.jqueryui.com/dialog/#event-beforeClose

来自网址:http: //api.jqueryui.com/dialog/#event-beforeClose

回答by Jeff Penner

If I understand correctly, you want to allow the user to click the 'X' button on the top right dialog, but you do not want to allow them to close the window. You probably want to trigger a different event instead.

如果我理解正确,您希望允许用户单击右上角对话框中的“X”按钮,但您不想让他们关闭窗口。您可能想要触发不同的事件。

Try this example out in your own code with your own dialogClass:

在您自己的代码中使用您自己的 dialogClass 试试这个例子:

$("#dialogId").dialog({
        dialogClass: "dialogId",
        title:     "someTitle",
        //modal:     true,
        //autoOpen:  false, 
        //resizable: false,
        //closeOnEscape: false,
        height:    500,
        width:     1000,
        open : function(event, ui){       
        },
        beforeClose: function (event, ui) {
            if ($(".dialogId .ui-dialog-titlebar-close").is(":focus")) { 
                alert('X clicked but do not close!');
                return false; // do not close dialog
            }
            return true; // close dialog
        }, 
        buttons: [
          { }
        ]
});

Essentially what's happening here is were are asking if the dialog's X button is being focused (a.k.a. clicked) and then we return false. You may trigger a different event here if you like, such as creating your own custom "Are you sure you want to cancel?" dialog popup on top.

本质上,这里发生的事情是询问对话框的 X 按钮是否被聚焦(也就是单击),然后我们返回 false。如果您愿意,您可以在此处触发不同的事件,例如创建您自己的自定义“您确定要取消吗?” 顶部弹出对话框。

Cheers! Good luck.

干杯! 祝你好运。

Jeffrey

杰弗里