开发者

jQuery :not and :first ordering

开发者 https://www.devze.com 2023-01-06 17:14 出处:网络
I am trying to get the first input field in a form that isn\'t disabled or hidden. I need to be able to exclude radio buttons, selects, and particular IDs.

I am trying to get the first input field in a form that isn't disabled or hidden. I need to be able to exclude radio buttons, selects, and particular IDs.

The following code works perfectly, EXCEPT when a select comes before the text or password field that I want:

$("form :input:visible:enabled:first:not('select')");

If I exclude the :not('select'), the select is returned as the first input field (correctly so, as :input returns pretty much all form elements). However, when I include the :not('select'), nothing is picked up. Any ideas?

Second part to this question - I assume it is ok to chain :nots so I could have something like:

$("form :input:vis开发者_StackOverflow社区ible:enabled:first:not('select'):not(#specific_id):not(type[radio])");


Would it work if you swap the :not and the :first?

`$("form :input:visible:enabled:not('select'):first");`

The way you have it, it selects the first enabled tag, which a set of just one. If that one is a select, you get nothing. With this version, it gets everything that isn't a select, and then takes the first element of that set.


Using Tesserex's answer, you can also join several not's.

$("form :input:visible:enabled:not('select, :radio, #id, .class'):first");

Just separate by commas as you would in any other jQuery tag.


it would be better to use the 'type=select' or 'type=xxx' in this case rather than the ':not' selector.

alternatively, you can use the .is() function.

0

精彩评论

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

关注公众号