2012-03-13 15 views
1

セレクタのdocumentationセレクタの:selectedセレクタでは、セレクタを使用する適切な方法は、たとえば$('select').filter(':selected')を使用することです。しかし、私はそれを動作させることはできません!それは、私が$('select :selected')のようなものを使用するときにのみ機能します。jquery:正しく選択されていませんか?

ここmy jsFiddle implementation of itとともに、最小の例は次のとおり

のjavascript:

function getLengths() { 
    $("#dothis").text($("select").filter(":selected").length); 
    $("#dothat").text($("select :selected").length); 
} 

$(function() { 
    getLengths(); 
    $("select").change(getLengths); 
}); 

関連HTML:

<select multiple="multiple"> 
    <option selected="true">one</option> 
    <option>two</option> 
</select> 
<div> 
    <pre>$("select").filter(":selected").length = <span id="dothis"></span></pre> 
    <pre>$("select :selected").length = <span id="dothat"></span></pre> 
</div> 

答えて

3

フィルタオプションの要素、選択しない:

function getLengths() { 
    $("#dothis").text($("option").filter(":selected").length); 
    $("#dothat").text($("select :selected").length); 
} 

$(function() { 
    getLengths(); 
    $("select").change(getLengths); 
}); 

http://jsfiddle.net/fdU83/6/

+0

アハ!それは理にかなっている! –

0

updated fiddle

:selectedがオンフィルタであります10

あなたはこれをしたい:より良い

$("select option").filter(":selected") 

それとも...

$("select option:selected") 
関連する問題