Check how many samples were fetched using jQuery

I have a choice:

<select name="paraquien" class="selectpicker form-control paraquien" id="paraquien" onchange="mostrarPreguntas();">
    <option value=""><?=__('¿Para quién es el plan?')?><span class="glyphicon glyphicon-triangle-bottom"></span></option>
    <option value="1"><?=__('Para mi')?> <span class="glyphicon glyphicon-triangle-bottom"></span></option>
    <option value="2"><?=__('Para regalar')?><span class="glyphicon glyphicon-triangle-bottom"></span></option>
</select> 

      

and I would like to know if they were all selected, in which case raise the event. I've tried this far:

jQuery('.paraquien option:selected')

      

Getting an array of results:

[
    <option value="1">​Para mi ​</option>​, 
    <option value="1">​Hombre​</option>​, 
    <option value="3">​Estudiante​</option>​,
    <option value>​Su situación sentimental​</option>​,
    <option value>​¿Tiene hijos?​</option>
​]

      

You can see that each selected parameter has a value attribute set, I would like to know how to get only the parameters that have already been set, in the same selector mentioned earlier.

Any idea?

+3


source to share


1 answer


You can use filter()

to check items select

where the value is still ''

. Try the following:

var $unchosenSelects = $('.paraquien').filter(function() {
    return $(this).val() == '';
});
if ($unchosenSelects.length) {
    // there was at least one select within nothing chosen...
}

      



In a similar way, you can use map()

to get all values ​​in an array and then $.inArray

check for empty strings:

var chosenValues = $('.paraquien').map(function() {
    return $(this).val();
});
if ($.inArray(chosenValues, '') != -1) {
    // there was at least one select within nothing chosen...
}

      

+2


source







All Articles