how to get selected option text in javascript

refs

https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/options

https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/selectedOptions

https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/selectedIndex

https://developer.mozilla.org/en/docs/Web/API/HTMLSelectElement

others

https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/item

question

solution

// e.target

temp1.options[temp1.selectedIndex].text;
// "五楼" temp1.selectedOptions[0].text;
// "五楼"

OK

There are two solutions, as far as I know.

both that just need using vanilla javascript

1 selectedOptions

<div class="select-box clearfix">
<label for="area">Area</label>
<select id="area">
<option value="101">A1</option>
<option value="102">B2</option>
<option value="103">C3</option>
</select>
</div>
const log = console.log;
const areaSelect = document.querySelector(`[id="area"]`); areaSelect.addEventListener(`change`, (e) => {
// log(`e.target`, e.target);
const select = e.target;
const value = select.value;
const desc = select.selectedOptions[0].text;
log(`option desc`, desc);
});

2 options

<div class="select-box clearfix">
<label for="area">Area</label>
<select id="area">
<option value="101">A1</option>
<option value="102">B2</option>
<option value="103">C3</option>
</select>
</div>
const log = console.log;
const areaSelect = document.querySelector(`[id="area"]`); areaSelect.addEventListener(`change`, (e) => {
// log(`e.target`, e.target);
const select = e.target;
const value = select.value;
const desc = select.options[select.selectedIndex].text;
log(`option desc`, desc);
});



最新文章

  1. 使用CSS3动画模拟实现小球自由落体效果
  2. 不同版本的name可以重复
  3. python paramiko ssh.exec_command()启动tomcat服务器应用进程失败问题解决方法- Neither the JAVA_HOME nor the JRE_HOME environment variable is defined At least one of these environment variable is needed to run this progr
  4. 关于高并发的aotomic
  5. (转)LR监控Linux系统性能计数器详解
  6. Mac下的eclipse 4.6的tomcat插件安装正确姿势
  7. iOS之GCDAsyncSocket第三方库的使用
  8. struts2中使用ognl表达式时各种符号的使用规则$,#,%
  9. linux下ffmpeg安装
  10. JVM学习记录
  11. Linux 基本概念和操作
  12. JS 数组位置方法 indexOf()和lastIndexOf()的理解
  13. Scala学习(二)练习
  14. innodb next-key lock引发的死锁
  15. linux服务器---代理认证
  16. C#完美读取CSV
  17. C++STL学习笔记_(2)deque双端数组知识
  18. 算法笔记_118:算法集训之结果填空题集二(Java)
  19. shell脚本中,for基于列表进行循环的实现方法
  20. C#后台获取ajax传来的xml格式数据值

热门文章

  1. 如何使用Set去重
  2. IdentityServer4之Implicit和纯前端好像很配哦
  3. mysql本地中127.0.0.1连接不上数据库怎么办
  4. [BJOI2016]水晶 做题心得
  5. docker镜像加速,docker更换为国内镜像
  6. Spring boot freemarker 配置
  7. shell (颜色输出)
  8. 5. Linux文件目录管理和打包压缩与搜索命令
  9. 你必须知道的关于操作系统的N个概念!
  10. allure生成的报告打开后显示loading