Jquery UI - 对话框关闭,按下了哪个按钮
我使用 jquery ui 对话框作为提示。 “提示”有“确定”和“取消”按钮。这里的问题是,当触发 .dialog("close") 时,它会提取对话框中的输入字段值,而我唯一的验证是输入字段的长度必须超过 0 个字符。这意味着即使您输入某些内容并按“取消”,提示中的文本也将被提交。我的想法是找出按下了什么按钮......有人知道这个问题的解决方案吗?
我当前的事件代码:
$("#addBusinessarea").click(function(){
createPrompt("Add new business area", "Business area name:");
$( "#prompt" ).bind( "dialogclose", function(event, ui) {
if($("#promptValue").val().length > 0){
// Add business area
}
});
});
I am using a jquery ui dialog as a prompt. The "prompt" has to buttons, "Ok" and "Cancel". The problem here is, it extracts the input fields value in the dialog when .dialog("close") is triggered and my only validation is that the length of the input field has to be more than 0 chars. This means that even when you type something and press cancel the text from the prompt will be submittet. My thought was to find out what button was pressed... Anyone know a solution to this?
My current event code:
$("#addBusinessarea").click(function(){
createPrompt("Add new business area", "Business area name:");
$( "#prompt" ).bind( "dialogclose", function(event, ui) {
if($("#promptValue").val().length > 0){
// Add business area
}
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要正确解决问题,请更改为 jQuery UI 对话框定义按钮的方式。它看起来像这样(请注意,您可以为不同的按钮使用不同的单击处理程序):
To properly solve change the way you've defined buttons for jQuery UI dialog. It can look like this (notice that you can have different click handlers for different buttons):
老问题,但这个问题出现在我面前,我在 jQuery UI Dialog 的帮助下找到了正确的答案Buttons
event.target
是您的按钮。Old question, but this question came up for me, and I found the right answer, with help from jQuery UI Dialog buttons
event.target
is your button.