How to get the # of options with the attr "selected" in a drop down using jQuery?
This isn't working for me:
开发者_运维百科 $('select').each(function() {
alert($(this).find('option').attr('selected').length);
}
actually what I really need is to just detect if the present select has any preselected options.
You can use the :selected selector:
alert($(this).find("option:selected").length);
Or, alternately, using this
as the selector's context:
alert($("option:selected", this).length);
精彩评论