开发者

Is there an OR combinator in jQuery/Sizzle? [duplicate]

开发者 https://www.devze.com 2023-04-08 03:41 出处:网络
This question already 开发者_StackOverflowhas answers here: Closed 11 years ago. Possible Duplicate:
This question already 开发者_StackOverflowhas answers here: Closed 11 years ago.

Possible Duplicate:

jQuery OR Selector?

I would like to for each select in the matched set find the first option that either has a class placeholder or an empty value something along the lines of

$(selectorOrJquery).find('select').andSelf().filter('select')
  .find('(option.placeholder OR option[value=""]):first)

That is unfortunately not a valid sizzle (or CSS3) syntax - anyone know if there is a valid way to do this with selectors?

Obviously I could write procedural code to find this for me, but I'm hoping for something a bit more slick

Edit: To clarify, I do not think a comma simply works. Imagine the following

<select>
  <option class="placeholder" value="0">Select Something</option>
  <option value="">Legitimate option that gets handled in JS</value>
</select>

In this case I want only the first selected, NOT both


I have also run into an issue like this in the past and settled for something like this.

$(selectorOrJquery).find('select').andSelf().filter('select')
  .find('option.placeholder, option[value=""]').eq(0);


This will return the first option of a select that is either empty or has a class placeholder.

$("select option:first").filter(":empty,.placeholder")


Just use a comma, which is a valid CSS/jQuery Selector.

$(selectorOrJquery).find('select').andSelf().filter('select')
  .find('(option.placeholder,  option[value=""]):first)


the css OR is the comma, so the selector you want would be

'option.placeholder:first, option[value=""]:first'
0

精彩评论

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

关注公众号