jquery,将焦点设置在页面上第一个启用的输入或选择或文本区域上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4328381/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:05:01  来源:igfitidea点击:

jquery, set focus on the first enabled input or select or textarea on the page

jqueryjquery-selectorsfocus

提问by Josh

It's fairly straightforward. I want to set focus to the first enabled and not hidden control on the page.

这是相当简单的。我想将焦点设置为页面上第一个启用而不是隐藏的控件。

For a textbox, I have

对于文本框,我有

$("input[type='text']:visible:enabled:first").focus();

But I want to get "all" form input controls: textbox, checkbox, textarea, dropdown, radio in my selector to grab the first enabled and not hidden control. Any suggestions?

但我想在我的选择器中获取“所有”表单输入控件:文本框、复选框、文本区域、下拉列表、单选框以获取第一个启用而不是隐藏的控件。有什么建议?

回答by Jordan Ryan Moore

$(':input:enabled:visible:first').focus();

回答by Dmitri Tsoy

As an extension of Jordan's answer

作为乔丹答案的延伸

$(':input:enabled:visible:not([readonly]):first').focus();

This one also excludes readonly inputs

这个也不包括只读输入

回答by zmonteca

This is a little more comprehensive:

这是一个更全面的:

$('form :input:text:visible:not(input[class*=filter]):first').focus();

Lamen: Focus the cursor in the first text input in a form that is visible, but if it use the class named "filter", ignore it!

Lamen:将光标聚焦在可见表单中的第一个文本输入中,但如果它使用名为“过滤器”的类,则忽略它!

回答by Breezer

Try this:

尝试这个:

$("input[type='text']:enabled","another selector","another selector").first().focus();