使用querySelectorAll获取选择的选项 [英] Getting selected options with querySelectorAll

查看:136
本文介绍了使用querySelectorAll获取选择的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在Javascript中是否有可能使用Selctors API在<select multiple>字段中获取当前选择的选项,而不是对所有选项进行愚蠢"的迭代.

I wonder if it's possible in Javascript to get the currently selected options in a <select multiple> field using the Selctors API rather than a "stupid" iteration over all options.

select.querySelectorAll('option[selected="selected"]')仅返回原始HTML中标记为预选的选项,这不是我想要的.有什么想法吗?

select.querySelectorAll('option[selected="selected"]') only returns the options that were marked as preselected in the original HTML, which is not what I'm looking for. Any ideas?

推荐答案

document.querySelectorAll('option:checked')

即使在IE9上也可以使用;)

Works even on IE9 ;)

这篇关于使用querySelectorAll获取选择的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆