gpt4 book ai didi

javascript - 获取 $(this) 的选定选项

转载 作者:行者123 更新时间:2023-11-30 07:16:39 29 4
gpt4 key购买 nike

如果我循环访问一些 jquery 对象,这些对象是 select 下拉列表,我如何使用 jquery 的 $(this) 获取所选选项的类?

elems.each(function(idx, elem) {
if ($(this).is('select')){
console.log($(this, "option:selected"))
}
});

似乎不起作用。

最佳答案

​elems.each(function(idx, elem) {
var ref = $(this); // caching $(this)
if( ref.is('select') ) {
console.log( ref.find('option:selected') );

// OR in jQuery Context format
console.log( $('option:selected', ref) ); // this format internally
// called .find() method

// to get the class
console.log( ref.find('option:selected').attr('class') );
}
})​;

关于javascript - 获取 $(this) 的选定选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13919911/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com