<select> 的 <option> 框的 jQuery 选择器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4261955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:57:15 来源:igfitidea点击:
jQuery selector for <select>'s <option> box
提问by FFish
Given the HTML below, I need a jQuery selector that selects the <option>
with the with-stamp
class.
下面给出的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
});
回答by PeeHaa
$("select[name='preset_select']").change(function() {
$(this).children(':selected').hasClass('with-timestamp')
}
回答by Nick Craver
You need to check only the :selected
<option>
(since .hasClass()
returns true
if anyof the elements in the set have the class), like this:
你需要检查只(因为返回如果任何集合中的元素有类),如下所示::selected
<option>
.hasClass()
true
$("select[name=preset_select] option:selected").hasClass("with-stamp")
回答by Ali Tarhini
$("select[name=preset_select] option:selected").hasClass('with-stamp').change(function() {
});