如何在对话框打开时模糊第一个表单输入

can*_*man 3 forms jquery jquery-ui datepicker

我有一个显示表单的jQuery.dialog.
第一个输入是<input type="text" class="datepicker" />,我像这样初始化datepicker jQuery('.datepicker').datepicker().

问题是当对话框打开时,它会聚焦第一个输入.所以datepicker也打开了.

对话框的事件open在焦点开始之前运行.

那么,我怎样才能模糊第一个焦点到日期选择器保持隐藏状态?

sil*_*ire 9

从jQuery UI 1.10.0开始,您可以使用HTML5属性 自动聚焦选择要关注的输入元素.

您所要做的就是在对话框中创建一个虚拟元素作为第一个输入.它将为您吸收焦点.

<input type="hidden" autofocus="autofocus" />
Run Code Online (Sandbox Code Playgroud)

这已在2013年2月7日在Chrome,Firefox和Internet Explorer(所有最新版本)中进行了测试.

http://jqueryui.com/upgrade-guide/1.10/#added-ability-to-specify-which-element-to-focus-on-open


BMi*_*ner 6

如前所述,这是jQuery UI的一个已知错误,应该很快就会修复.直到那时...

这是另一种选择,所以你不必乱用tabindex:

暂时禁用日期选择器,直到对话框打开:

dialog.find(".datepicker").datepicker("disable");
dialog.dialog({
    "open": function() {$(this).find(".datepicker").datepicker("enable");},
});
Run Code Online (Sandbox Code Playgroud)

适合我.