(JQuery-UI) 有没有办法检测对话框的 beforeClose() 处理程序中按下了哪个按钮?
我的 JavaScript 代码打开一个对话框,请求用户提供一些信息,以及两个按钮(确定和取消)。我想使用 dialog
的 beforeClose
处理程序在用户单击“确定”按钮时验证输入(这样,如果用户没有进行正确的输入,他/她仍然在对话框“中”并且有机会纠正它)。有没有办法知道按下了两个按钮中的哪一个? (显然,如果用户按下“取消”,则不需要输入验证,对话框可以继续并关闭 - 即我的 beforeClose
可以返回 true
)。我看到 beforeClose
需要两个输入,event
和 ui
但这些似乎都不包含我想要的信息。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需在关闭框之前绑定一些其他函数(如果表单未验证,则根本不调用关闭)。只需在 $(this).dialog("close") 之前添加所需的任何检查或 ajax 调用即可。
来自:http://jqueryui.com/demos/dialog/#modal-confirmation
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
我在我的一个应用程序中都有这些,而不是在自定义验证中闲逛,我只是抛出一个 内联验证在对话框中的表单上。它会为您进行拦截,轻松验证字段,如果正确,就让它飞。如果没有,它会自动进行通知。
您当然可以自制这个解决方案,但有人已经为您完成了这项工作。保存您的代码以应对更大的问题。
如果您决定自己执行此操作,只需按照文档中的说明将操作放入按钮功能中即可。
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.