Javascript 关注第一个表单元素,不管它是什么。jQuery对话框

Javascript 关注第一个表单元素,不管它是什么。jQuery对话框,javascript,jquery,forms,jquery-ui,Javascript,Jquery,Forms,Jquery Ui,我有一个jQuery模态对话框,当它打开时,我希望它集中在第一个表单元素上 目前,我有以下几点: the_dialog.dialog({ modal: true, width: 700, title: 'title', close: suicide , open: function(event, ui) { setTimeout(function() {

我有一个jQuery模态对话框,当它打开时,我希望它集中在第一个表单元素上

目前,我有以下几点:

the_dialog.dialog({ 
        modal: true, 
        width: 700,
        title: 'title',
        close: suicide ,
        open: function(event, ui) { 
                setTimeout(function() {
                    jQuery('#').focus(); <-- VERY SPECIFIC CSS SELECTOR PERHAPS?
                }, 220);     
            } 
    }
);

在不添加任何类或ID的情况下,当表单打开时,我是否可以关注第一个表单元素,不管它是什么?

是否可以使用标记来包装标签

那你可以用

$('table').find('td').eq(0).children().eq(0).focus()
虽然有点冗长,但如果孩子是输入、选择或其他任何文本区域等,都可以使用

jQuery('input,select').first().focus();
关于注释,您应该确定其范围,使其仅适用于对话框,例如

可以在打开的回调中使用psuedo:input选择器并查找第一个非隐藏元素

open: function(event, ui) { 
       the_dialog.find(':input:not(:hidden):first').focus()     
 }
:输入筛选标记,并

:hidden筛选从display:none和type=hidden中看不到的任何标记。您可以使用:input选择器在对话框中查找第一个表单元素:

the_dialog.find(':input:first').focus()
我建议:

// I'm assuming that <textarea> elements may be used, they can be removed if not;
// 'dialog' is an assumed reference to a jQuery object containing the relevant <table>:
dialog.find('input, select, textarea')
  // retrieving the first element matched by the selector:
  .eq(0)
  // focusing that found element:
  .focus();
//这一部分完全无关,仅用于改变第一个表单元素, //为了证明该方法的有效性,无论哪个要素是“第一要素”: var formElements=[,]; $'td:nth-child2'.htmlfunctioni{ 返回$formElements[Math.floorMath.random*formElements.length].vali; }; //上文解释的相关部分: $'input,select,textarea'.eq0.focus; 标签 标签 标签 标签
我假设jQuery.focus;您要调整的行是吗?谢谢,但恐怕我无法进行更改。如果主页中有输入怎么办?这不会隔离对话框中的元素。据我所知或记忆,我没有使用jQuery模式,我不确定这是否会引用模式。不过,我已经修改了解释代码中的选择器,以尝试显示上下文的需要/使用。非常感谢。
// I'm assuming that <textarea> elements may be used, they can be removed if not;
// 'dialog' is an assumed reference to a jQuery object containing the relevant <table>:
dialog.find('input, select, textarea')
  // retrieving the first element matched by the selector:
  .eq(0)
  // focusing that found element:
  .focus();