2010-11-23 8 views
15

以下のHTMLを指定すると、with-stampクラスを持つ<option>を選択するjQueryセレクタが必要です。jQueryセレクタ<select>の<option>ボックス

<select name="preset_select"> 
    <option selected="selected" value="original">ORIGINAL</option> 
    <option value="large">LARGE</option> 
    <option class="with-stamp" value="medium">MEDIUM</option> 
</select> 

$("select[name=preset_select]").change(function() { 
    // console.log($(this).hasClass("with-stamp")); // all false 
    // console.log($("select[name=preset_select] option").hasClass("with-stamp")); // all true 
}); 

答えて

21
$("select[name='preset_select']").change(function() { 
    $(this).children(':selected').hasClass('with-timestamp') 
} 
+0

ありがとう、まず私は動作しないと思ったが、私はあなたが私のクラス名を変更したことを見た:-) – FFish

+0

おかげで申し訳ありません。 あなたはそれを理解してうれしい:) – PeeHaa

3

かを確認する必要がある唯一の:selected<option>.hasClass()返すのでtrueセット内の要素のいずれかの場合、クラスを持っている)このように、:

$("select[name=preset_select] option:selected").hasClass("with-stamp") 
+1

私はPeeHaaの方が$(this)を使用するので好きです。 – FFish

2
$("select[name=preset_select] option:selected").hasClass('with-stamp').change(function() { 

}); 
+0

'.class'セレクタがありますが、それ以外に' change'イベントはありません'