(JQuery-UI) 有没有办法检测对话框的 beforeClose() 处理程序中按下了哪个按钮?

发布于 2024-12-27 05:18:48 字数 368 浏览 0 评论 0原文

我的 JavaScript 代码打开一个对话框,请求用户提供一些信息,以及两个按钮(确定和取消)。我想使用 dialogbeforeClose 处理程序在用户单击“确定”按钮时验证输入(这样,如果用户没有进行正确的输入,他/她仍然在对话框“中”并且有机会纠正它)。有没有办法知道按下了两个按钮中的哪一个? (显然,如果用户按下“取消”,则不需要输入验证,对话框可以继续并关闭 - 即我的 beforeClose 可以返回 true)。我看到 beforeClose 需要两个输入,eventui 但这些似乎都不包含我想要的信息。

My JavaScript code puts up a dialog box requesting some information from a user, as well as two buttons (OK and Cancel). I would like to use dialog's beforeClose handler to validate input when a user clicks the OK button (so that if the user didn't make the proper input, he/she is still "in" the dialog box and has a chance to correct it). Is there a way of telling which of my two buttons was pressed? (obviously if the user pressed Cancel, then no input validation is necessary and the dialog box can just go ahead and close - i.e. my beforeClose can return true). I see that beforeClose takes two inputs, event and ui but neither of these appear to contain the information I want.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空宴 2025-01-03 05:18:48

您只需在关闭框之前绑定一些其他函数(如果表单未验证,则根本不调用关闭)。只需在 $(this).dialog("close") 之前添加所需的任何检查或 ajax 调用即可。
来自:http://jqueryui.com/demos/dialog/#modal-confirmation

<script>
    $(function() {
        // a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
        $( "#dialog:ui-dialog" ).dialog( "destroy" );

        $( "#dialog-confirm" ).dialog({
            resizable: false,
            height:140,
            modal: true,
            buttons: {
                "Delete all items": function() {
                    $( this ).dialog( "close" );
                },
                Cancel: function() {
                    $( this ).dialog( "close" );
                }
            }
        });
    });
    </script>

You simply bind some other functions prior to closing the box (and don't call the close at all if the form doesn't validate). Just add whatever check or ajax calls you need before the $(this).dialog("close").
From: http://jqueryui.com/demos/dialog/#modal-confirmation

<script>
    $(function() {
        // a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
        $( "#dialog:ui-dialog" ).dialog( "destroy" );

        $( "#dialog-confirm" ).dialog({
            resizable: false,
            height:140,
            modal: true,
            buttons: {
                "Delete all items": function() {
                    $( this ).dialog( "close" );
                },
                Cancel: function() {
                    $( this ).dialog( "close" );
                }
            }
        });
    });
    </script>
寻找我们的幸福 2025-01-03 05:18:48

我在我的一个应用程序中都有这些,而不是在自定义验证中闲逛,我只是抛出一个 内联验证在对话框中的表单上。它会为您进行拦截,轻松验证字段,如果正确,就让它飞。如果没有,它会自动进行通知。

您当然可以自制这个解决方案,但有人已经为您完成了这项工作。保存您的代码以应对更大的问题。

如果您决定自己执行此操作,只需按照文档中的说明将操作放入按钮功能中即可。

I have these all over one of my apps, and rather than dinking around with custom validation I just throw in an inline validation on the form in the dialog. It will do the intercept for you, easily validate the fields, and if it's correct, let it fly. If not, it'll automatically do the notification.

You certainly COULD home-brew this solution, but someone's already done the work for you. Save your code for a bigger problem.

If you were determined to do it yourself, simply put the action in the button function as described in the documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文