Choose not selected

I have a form with 4 dropdowns. The default option in all drop-down lists is “Please select.” I want to use jquery to make sure all the dropdowns matter before submitting the page, but my code only works on the first. Does anyone have a way that I can immediately check all the drop down lists?

function doconfirm() {
  if ($('select').val() == '') {
    alert("Please Select All Fields");
   }  
}

I am sure something is missing, but I cannot figure it out. Thanks

+5
source share
4 answers

function doconfirm() {
  if ($('select').each(function() {
    if($(this).val() == '') {
         alert("Please Select All Fields");
         return(false);
    }  
  });
}
+8
source

@mgroves, , , select, . - , (, .val() ):

function doconfirm() {
  if($('select').each(function() {
    if(!$(this).find('option:selected').length) {
         alert("Please Select All Fields");
         return(false);
    }  
  });
}
+4

for a different approach to this, why not use

function doConfirm() {

    var toReturn = true;

    if ( $('select').filter( function(){
            return $(this).val() === ''; 
         }).length ) {

        toReturn = false;
        //notify user

    }

    return toReturn;
}
+1
source
$('select').each(function(s){if(!this.val())...});
0
source

All Articles