-4

Possible Duplicate:
Check if option is selected with jQuery, if not select a default

I have html like this:

<div id="somedivid">

<select class="someclass">
<!-- options -->
</select>

<select class="someclass">
<!-- options -->
</select>

</div>

How to check if options is selected in both select boxes?

EDIT: i cant change html, so it means i cant add ids to select elements.

Community
  • 1
  • 1
SomeoneS
  • 1,207
  • 2
  • 19
  • 34
  • 1
    what i did was wrote your questions topic + 'jquery' to google, and [look what i found](http://stackoverflow.com/questions/149573/check-if-option-is-selected-with-jquery-if-not-select-a-default) – Tom Sep 21 '12 at 12:11
  • @Tom well, you found solution when there is only one select element. I know how to do that. – SomeoneS Sep 21 '12 at 12:16

4 Answers4

3

A select always has a selection.

But supposing you put an empty choice like this :

​<select id=select1>
    <option></option>
    <option>A</option>
    <option>B</option>
</select>​

You can test this :

if ($('#select1').val() && $('#select2').val()) {
   // both have selection

EDIT : if you don't have ids or names, you may use this :

var allSelected = true:
$('#somedivid select').each(function() {
    if (!$(this).val()) {
       allSelected = false;
       break;
    }
});
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • Yeah, i know to do that, but my select elements does not have ids, only classes that are same for both elements. And i cant change html. – SomeoneS Sep 21 '12 at 12:17
  • I edited for a solution if you don't have ID or name. But you must define what does mean that something is selected (for example empty choice). – Denys Séguret Sep 21 '12 at 12:19
0
<script type="text/javascript">
    $( function() { 
        $( '#somedivid > select' ).bind( 'change', function(){
            if( $( '#sel1').attr( 'value' ) != 'default' && $( '#sel1' ).attr( 'value' ) != 'default' ){
                //do something here.
            }
        });
    } );
</script>
<div id="somedivid">

    <select id="sel1">
        <option value="default" selected="selected"></option>
        <!-- options -->
    </select>

    <select id="sel2">
        <option value="default" selected="selected"></option>
        <!-- options -->
    </select>

</div>
OctoD
  • 365
  • 5
  • 13
0

Considering both selectbox has some default option value ="none"

var isBothSelected=0;
if($('#selectbox1 option:selected').val() =='none'){
    alert('Plase select the select1');
    isBothSelected=isBothSelected+1;
    //return false;
}
if($('#selectbox2 option:selected').val() =='none'){
    alert('Plase select the select2');
   isBothSelected=isBothSelected+1;
    // return false;
}
if(isBothSelected ==2){
  alert('Both are not selected')
}
Prathap
  • 717
  • 2
  • 8
  • 25
0

Try this

      var val=  $("#selectid option:selected").value();
           if(val==="")
               {
          alert("your code on not selected condition");
           }
Aravindhan
  • 3,566
  • 7
  • 26
  • 42