【发布时间】:2010-03-26 09:23:34
【问题描述】:
在 jquery 模态对话框中,有没有办法选择一个按钮作为默认操作(用户按下回车时执行的操作)?
jquery 网站示例: jquery dialog modal message
在上面的示例中,当用户按下 Esc 时对话框关闭。我希望在用户按下 Enter 时调用“确定”按钮操作。
【问题讨论】:
标签: javascript jquery jquery-ui modal-dialog
在 jquery 模态对话框中,有没有办法选择一个按钮作为默认操作(用户按下回车时执行的操作)?
jquery 网站示例: jquery dialog modal message
在上面的示例中,当用户按下 Esc 时对话框关闭。我希望在用户按下 Enter 时调用“确定”按钮操作。
【问题讨论】:
标签: javascript jquery jquery-ui modal-dialog
在对话框的打开功能中,您可以聚焦按钮:
$("#myDialog").dialog({
open: function() {
$(this).parents('.ui-dialog-buttonpane button:eq(0)').focus();
}
});
如果 :eq(0) 位于不同的索引处,则更改它,或者按名称查找等。
【讨论】:
function() { $(this).parent().find('.ui-dialog-buttonpane button:eq(1)').focus(); }.
我喜欢这个(它对我有用),它将焦点留在我想要的位置(文本框)
$("#logonDialog").keydown(function (event) {
if (event.keyCode == $.ui.keyCode.ENTER) {
$(this).parent()
.find("button:eq(0)").trigger("click");
return false;
}
});
但是,这仅适用于一个按钮(确定按钮),如果需要,可以设置 ':eq(n)' 以选择其他按钮。
注意:我添加了一个返回false的新行,以防止在处理回车键时出现事件冒泡,希望它比以前更好。
【讨论】:
$(this).parent().find('button:nth-child(1)').click(); 看起来更近的元素已经占据了第零位!
试试这个方法:
$("#myDialog").dialog({
open: function() {
$(this).siblings('.ui-dialog-buttonpane').find('button:eq(1)').focus();
}
});
【讨论】:
This other stackoverflow question 应该可以带你去你想去的地方:
$('#DialogTag').keyup(function(e) {
if (e.keyCode == 13) {
//Close dialog and/or submit here...
}
});
【讨论】:
另一个让您可以更好地控制对话框中所有按钮的选项是将它们添加为按钮数组。然后在 open 事件中,您可以通过 id 获取按钮并做任何您想做的事情(包括设置焦点)
$('#myDialog').dialog({
buttons: [
{
id: "btnCancel",
text: "Cancel",
click: function(){
$(this).dialog('close');
}
},
{
id: "btnOne",
text: "Print One",
click: function () {
SomeFunction(1);
}
},
{
id: "btnTwo",
text: "Print Two",
click: function(){
SomeFunction(0);
}
}
],
open: function () {
if ($('#hiddenBool').val() != 'True') {
$('#btnOne').hide();
}
$("#btnTwo").focus();
}
});
【讨论】:
使用按钮名称作为选择器的细微变化。它读起来好一点,但按钮文本字符串明显重复。重构以适应口味。
$("#confirm-dialog").dialog({
buttons: {
"Cancel" : function(){},
"OK" : function(){}
},
open: function() {
$(this).siblings('.ui-dialog-buttonpane').find("button:contains('OK')").focus();
}
});
【讨论】:
不过,最简单的方法是在对话框中的表单上使用提交操作:
我工作的公司是“EBL”,我避免使用全局范围...因此以下函数的前缀:
EBL.onUiDialogOpen = function (event, ui, hideX, actionFirstButtonOnEnterKey) {
if (hideX) {
// There is no option to hide the 'X' so override.
$(".ui-dialog-titlebar-close").hide();
}
if (actionFirstButtonOnEnterKey) {
/* (event.target) will give the div that will become the content
of a UI dialog, once div is 'opened' is it surrounded by a
parent div that contains title and buttonpane divs as well as
content div - so I use .parent()
...The keyup function is binded to all descendants, therefore:
-We need the e.stopPropagation() line.
-This code is NOT what you want if you DON'T want enter
key to initiate first button regardless of selected control.
*/
$(event.target).parent().bind('keydown.justWhileOpen', function (e) {
if (e.keyCode === $.ui.keyCode.ENTER) {
e.stopPropagation();
$(event.target).next('.ui-dialog-buttonpane')
.find('button:first').click();
}
});
}
};
...结合使用:
EBL.onUiDialogClose = function (event, ui) {
// Remove keyup handler when we close dialog
$(event.target).parent().unbind('.justWhileOpen');
};
如果您使用动态创建的 div 并在之后销毁它,则不需要 .onUiDialogClose。
您可以在下面看到我在初始化非动态对话框时如何使用这些库函数...
$('#divName').dialog({
//...
open: function (event, ui) { EBL.onUiDialogOpen(event, ui, false, true); },
close: function (event, ui) { EBL.onUiDialogClose(event, ui); },
//...
});
到目前为止,我已经在 IE9 和最新的 chrome/firefox 中对此进行了测试。 您应该在“确定”函数中验证对话框是否必要。
【讨论】:
.no-close .ui-dialog-titlebar-close { display: none; },然后像这样设置对话框的样式:$( "#dialog" ).dialog({ dialogClass: "no-close", ...
我使用的是 1.10.0 版。我无法让它以开放但专注的方式工作。这将聚焦第二个按钮:
focus: function(){
$(this).siblings('.ui-dialog-buttonpane').find('button:eq(1)').focus();
}
【讨论】:
$("#logonDialog").keydown(function (event) {if (event.keyCode == 13) {
$(this).parent().find("button:eq(0)").trigger("click");
return false;
}
});
【讨论】:
这在使用 jquery 1.10.2 的对话框中对我有用
dialog({
focus: function() {
$(this).on("keyup", function(e) {
if (e.keyCode === 13) {
$(this).parent().find("button:eq(1)").trigger("click");
return false;
}
});
},
更多选项...
【讨论】:
这段简单的代码为您的按钮设置样式并将默认设置为最后一个:
open: function(){
$buttonPane = $(this).next();
$buttonPane.find('button:first').addClass('accept').addClass('ui-priority-secondary');
$buttonPane.find('button:last').addClass('cancel').addClass('ui-state-default');
$buttonPane.find('button:last').focus();
},
【讨论】:
在我的例子中,没有一个答案有效,因为我在一个空的 div 上调用了 .dialog 并添加了我的按钮动态,所以 $(this).html() 不会返回任何内容.所以我不能调用像parent() 或siblings() 这样的方法并期望得到回报。我所做的是直接选择ui-dialog-buttonpane 类并从那里找到按钮元素
HTML
<div id = "dialogexample">
</div>
jQuery
$("#dialogexample").dialog({
autoOpen: false,
modal: true,
open: function () {
$('.ui-dialog-buttonpane').find('#otherbutton').focus();
}
});
var buttons = {
"MyButton" : {
text: "Do Stuff",
id: "dostuffbutton"
},
"OtherButton" : {
text: "Other Stuff",
id: "otherbutton"
}
}
$("#dialogexample").dialog("option", "buttons", buttons);
$("#dialogexample").dialog("open"); //the second (otherbutton), instead of
//the first (dostuffbutton) button should be focused
【讨论】:
我知道这是一个旧线程,但我一直在寻找这个确切的功能,并且能够实现我认为最好的解决方案,因为我发现以上所有内容都有点不足。
它是上面两个答案的组合。使用 ID 而不是依赖 find() 函数来查找按钮元素对我来说似乎总是一个更好的选择。
如果需要,还可以显式查找要按下的回车键,这样我们可以在打开对话框时将焦点设置到我们想要的任何元素。这似乎允许最大的灵活性,同时满足在按下回车键时将特定按钮作为“默认”触发的愿望。我还实现了“取消”默认值。
我希望这可以帮助其他人寻找一个好的对话框“默认”按钮解决方案。
$("#LoginBox").dialog({
open: function(){
$(this).keydown(function (event) {
if (event.keyCode == 13) {
$("#LogInButton").trigger("click");
return false;
}
if (event.keyCode == 27) {
$("#CancelButton").trigger("click");
return false;
}
});
},
close: function(){
$(this).dialog("destroy");
},
buttons: [
{
id: "LogInButton",
text: "Log In",
click: function(){
//button functionality goes here
$(this).dialog("destroy");
}
},
{
id: "CancelButton",
text: "Cancel",
click: function(){
$(this).dialog("destroy");
}
}
]
});
【讨论】:
您应该使用 :tabbable 选择器和按钮的索引(0 是 [X] 按钮,您的从 1 开始)
open: function() {
var tb = $(":tabbable", this.parentNode);
if(tb.length>1) {
tb[1].focus();
}
}
【讨论】: