<select>的<option>框</option> </select>的jQuery选择器

时间:2010-11-23 23:07:39

标签: jquery jquery-selectors

鉴于下面的HTML,我需要一个jQuery选择器,用<option>类选择with-stamp

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

3 个答案:

答案 0 :(得分:26)

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

答案 1 :(得分:3)

您需要仅检查:selected <option>(因为.hasClass()返回true,如果集合中的任何元素都有类),像这样:

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

答案 2 :(得分:2)

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

}); 
相关问题