filter(), selects, , map(), :
var values = $('select[name="test[]"]').filter(function() {
return $.trim(this.value).length;
}).map(function() {
return this.value;
}).get();
Obviously, with this, you will not know what choice matters, if you need to know, you will need to use the element index as the only unique identifier (given your markup).
Next, an array of objects containing the index of the elements and its value will be returned:
var values = $('select[name="test[]"]').filter(function() {
return $.trim(this.value).length;
}).map(function() {
return { index: $(this).index(), value: this.value };
}).get();
To check if something has been selected, you simply set values.length:
if (!values.length) {
alert('Nothing was selected');
}
source
share