Nithish
Nithish

Reputation: 253

JQuery Selector code validation for list of select options

I'm having situation like 3 select box is there in a form

    <form>
   <select name="select1">
 <option value="a">a</option>
 <option value="b">b</option>
 <option value="c">c</option>
 </select>

<select name="select2">
 <option value="a">a</option>
 <option value="b">b</option>
 <option value="c">c</option>
 </select>
  <select name="select3">
 <option value="a">a</option>
 <option value="b">b</option>
 <option value="c">c</option>
 </select> </form>

On submitting this form i have to check atleast one of the select box value should be "a".

Is there any one line selctor query like jQuery['form'].find('select val=="a"').length() like this .

Thanks, Nithish.

Upvotes: 1

Views: 509

Answers (3)

jAndy
jAndy

Reputation: 236092

If I understand you right, and you want to know if at least one selected option has the value of 'a', it should look like

if( $('form').find('option:selected[value=a]').length ) {
   /*  */
}

Upvotes: 1

jgradim
jgradim

Reputation: 2871

Try this: $('form#yourform option:selected[value="a"]').length > 0

Upvotes: 1

user113716
user113716

Reputation: 322542

Example: http://jsfiddle.net/patrick_dw/Zvmfg/

jQuery('form select option:selected[value="a"]').length;

So you could do:

if( jQuery('form select option:selected[value="a"]').length ) {
    alert( 'at least one was selected' );
} else {
    alert( 'no "a" values selected' );
}

Upvotes: 2

Related Questions