jQuery selector for <select>'s <option> box
Given the HTML below, I need a jQuery selector that selects the <option>
with the with-stamp
class.
<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("wit开发者_Python百科h-stamp") ); // all false
// console.log( $("select[name=preset_select] option").hasClass("with-stamp") ); // all true
});
$("select[name='preset_select']").change(function() {
$(this).children(':selected').hasClass('with-timestamp')
}
You need to check only the :selected
<option>
(since .hasClass()
returns true
if any of the elements in the set have the class), like this:
$("select[name=preset_select] option:selected").hasClass("with-stamp")
$("select[name=preset_select] option:selected").hasClass('with-stamp').change(function() {
});
精彩评论