开发者

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜