jquery, set focus on the first enabled input or se

2019-02-04 00:09发布

问题:

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?

回答1:

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


回答2:

As an extension of Jordan's answer

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

This one also excludes readonly inputs



回答3:

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!



回答4:

Try this:

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