user1937021 user1937021 - 15 days ago 9
jQuery Question

Check if any select boxes have not been selected

I have a dynamic number of select elements. When the form is submitted, I want to check if any of them have not been selected yet ie the

value == ""
, if so throw an error, any ideas how I do this please in Jquery? Thanks!

<div class="container-seller-commission">
<div class="row-seller-commission">
<select name="seller" id="seller" class="select-seller valid">
<option value=""></option>
<option value="4" selected="selected">Agent</option>
<option value="2">Crisalix Global</option>
<option value="1">Owner</option>
<option value="3">Support</option>
</select>
</div>
</div>
<div class="row-seller-commission">
<select name="seller" id="seller" class="select-seller valid">
<option value=""></option>
<option value="4" disabled="disabled">Agent</option>
<option value="2" selected="selected">Crisalix Global</option>
<option value="1">Owner</option>
<option value="3">Support</option>
</select>
</div>
<div class="row-seller-commission">
<select name="seller" id="seller" class="select-seller valid">
<option value=""></option>
<option value="4" disabled="disabled">Agent</option>
<option value="2">Crisalix Global</option>
<option value="1" selected="selected">Owner</option>
<option value="3" disabled="disabled">Support</option>
</select>
</div>

Answer

Here is the working fiddle

And here is the code used in the Fiddle,

$(document).ready(function(){

    $("#validateButtonId").on("click",function(){

    var selectBoxFlag = true;

    //iterating through all the select boxes with class 'select-seller'
    $(".select-seller").each(function(){
      if($(this).val() == ""){
        selectBoxFlag = false;
      }
    });

    if(selectBoxFlag){
        $("#formId").submit();
    }else{
        alert("Selectbox value is not selected!");
    }

  });
});

The "validateButtonId" is the ID of the button that you use when you try to submit the form, keep in mind that the input type of this button should be button and not submit