在 jQuery UI 对话框中触发按钮单击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7530980/
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
Trigger a button click inside a jQuery UI Dialog
提问by mohamnag
it is a very simple question that I'm not finding an answer for it. I have a dialog and in some events happening inside the dialog I want to click one of the dialog buttons. The code which defines the dialog is:
这是一个非常简单的问题,我没有找到答案。我有一个对话框,并且在对话框内发生的某些事件中,我想单击其中一个对话框按钮。定义对话框的代码是:
var dialog = $('<div>').dialog({
autoOpen: false,
title : title,
resizable : false,
buttons : {
'CANCEL' : {
text : messages.Cancel,
click : function(){$(this).dialog('close')}
},
'OK' : {
text : messages.Ok,
click : okButtonCallback
}
}
});
and in my event I can get the dialog, find the buttons but I can not trigger the click event with right reference passed as this. I do this:
在我的事件中,我可以获得对话框,找到按钮,但我无法通过正确的引用来触发点击事件。我这样做:
buttons = dialog.dialog('option', 'buttons');
and I have the buttons each of them has the click function. If called directly or through trigger('click'), they call the click event of button but with the button itself as this not the dialog object. I saw somewhere to call
我有每个按钮都有点击功能。如果直接调用或通过 trigger('click') 调用,它们会调用按钮的单击事件,但按钮本身不是对话框对象。我看到了可以打电话的地方
buttons['OK'].apply(dialog);
but my buttons have absolutely no apply function!
但是我的按钮完全没有应用功能!
I'm not sure what can I do!
我不知道我能做什么!
采纳答案by Shlomi Komemi
First of all, you need to get buttons[0]
not buttons['OK']
, then, it's not a function it's an object, try to get to click function like this :
首先,你需要得到buttons[0]
not buttons['OK']
,然后,它不是一个函数,它是一个对象,尝试像这样点击函数:
buttons[0].click.apply(dialog);
回答by aziz punjani
$('.ui-button:contains("Ok")').click()
回答by Ailton F. Silva
What I use is:
我使用的是:
// Get the buttons
var buttons = $("#myDialog").dialog("option", "buttons");
// Calls the event
buttons["OK"]();