我目前正在使用jquery UI模态框。
我想知道如何在打开对话框时将焦点设置到模态框的第一个表单元素上。我认为这应该是默认情况下发生的,但由于某种原因它没有发生。
如何设置jquery ui在打开时将焦点设置在第一个表单元素上?
以下是带有模态对话框的页面的网址,只需单击“Show the Dialog”链接即可:此页面
我目前正在使用jquery UI模态框。
我想知道如何在打开对话框时将焦点设置到模态框的第一个表单元素上。我认为这应该是默认情况下发生的,但由于某种原因它没有发生。
如何设置jquery ui在打开时将焦点设置在第一个表单元素上?
以下是带有模态对话框的页面的网址,只需单击“Show the Dialog”链接即可:此页面
你可以利用jQuery UI对话框中的打开事件,并将焦点设置到输入框ID。你可以像这样做。
$( ".selector" ).dialog({
open: function(event, ui) { $('#target').focus(); }
});
添加一个函数到"shown"事件,然后设置焦点。
$('#login_modal').on('shown', function () {
$("#login_modal input").first().focus();
});
$('#login_modal').modal();
感谢回复,最终我不得不使用事件回调函数'shown.bs.modal'来将焦点添加到元素。
$('#login-modal').on('shown.bs.modal', function () {
$("#user_session_email").focus();
});
默认情况下,jQuery UI 模态框将聚焦于模态框中的第一个输入框。
如果由于某种原因第一个输入框没有聚焦,您可以在第一个输入框上添加 autofocus 属性:
<input type="text" name="date" value="" autofocus>
<input type="text" name="phone" value="">
或者如果你需要第二个或者其他输入框获得焦点,那么就将 autofocus 属性应用于第二个输入框:
<input type="text" name="date" value="">
<input type="text" name="phone" value="" autofocus>
:)
<!doctype html>
了吗?你的页面上有其他JS代码在聚焦吗?另外,你是否正在使用最新版本的jQuery和jQuery UI Dialog? - Jonathan MarzullosetTimeout(function () { $('#cntrlId').focus(); }, 1);
在打开对话框上添加功能
$("#dialogMensaje").dialog({width: 600,
title: "Notificación",
modal: true,
buttons: {
"Enviar": function() {
$(this).dialog("close");
}
},
open: function() {
setTimeout(function() {
$('#txt').focus();
}, 420);
}
});
显示对话框需要约460毫秒,因此最好使用
setTimeout,500
setTimeout(function(){$("#target").focus();},500);