开发者

Wanted: 'Super selector' or How focus the first visible "input" field that has no value?

I was wondering if it is possible to focus the fi开发者_开发百科rst empty visible "input" field with only one jQuery selector: something like $('superamazingselector').focus();

With "input" fields I mean: text areas, inputs (not type="hidden"), selects, radio buttons, unchecked checkbox fields. I guess this 'super selector' would be very handy on many pages.


Try this (untested):

$(':input[value=""]:visible:not(:button):first, :checkbox:not(:checked):first').first().focus();
  • :input - pseudo-class selector for all form input elements (includes <textarea> and <select>)

  • [value=""] - value attribute is empty

  • :visible - filters out hidden elements (doesn't select <input type="hidden" />)

  • :not(:button) - :not() a <button> or <input type="button" />

  • :checkbox - selects checkboxes

  • :not(:checked) - checkboxes that are :not() checked

How you select radio buttons depends on your HTML structure for <input type="radio" />, I can't think of a one-size-fits-all selector that detects an unselected radio button group.


Nice question:

Give this a try:

$(':input[value=""]:visible:first').focus();


I think $(":input:visible[value='']").first() should do it.

example: http://www.jsfiddle.net/U3r6C/1/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜