开发者

How to get the # of options with the attr "selected" in a drop down using jQuery?

开发者 https://www.devze.com 2023-03-24 18:44 出处:网络
This isn\'t working for me: 开发者_运维百科$(\'select\').each(function() { alert($(this).find(\'option\').attr(\'selected\').length);

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消