You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

技术求助:如何获取select字段的当前选中值?

获取select字段当前选中值的几种方法

原生JavaScript实现

  • 利用selectedIndex获取
    先定位select元素,通过selectedIndex拿到选中项的索引,再提取对应的值或文本:
// 替换成你的select元素ID
const select = document.getElementById('your-select-id');
// 获取选中值
const selectedValue = select.options[select.selectedIndex].value;
// 如需获取选中的文本内容
const selectedText = select.options[select.selectedIndex].text;
  • 直接读取select元素的value属性
    现代浏览器支持直接通过select元素的value属性获取当前选中值,写法更简洁:
const select = document.getElementById('your-select-id');
const selectedValue = select.value;
  • 通过选中状态查询option元素
    使用querySelector定位被选中的option,再提取值:
const selectedOption = document.querySelector('#your-select-id option:checked');
// 处理未选中的边界情况
const selectedValue = selectedOption ? selectedOption.value : null;

jQuery实现(若项目使用jQuery)

如果你的项目依赖jQuery,可以用更简洁的写法:

// 获取选中值
const selectedValue = $('#your-select-id').val();
// 获取选中文本
const selectedText = $('#your-select-id option:selected').text();

内容的提问来源于stack exchange,提问作者Patryk Gmyrek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 06:09:57