how to get selected option text in javascript
2024-09-03 20:16:42
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);
});
最新文章
- 使用CSS3动画模拟实现小球自由落体效果
- 不同版本的name可以重复
- 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
- 关于高并发的aotomic
- (转)LR监控Linux系统性能计数器详解
- Mac下的eclipse 4.6的tomcat插件安装正确姿势
- iOS之GCDAsyncSocket第三方库的使用
- struts2中使用ognl表达式时各种符号的使用规则$,#,%
- linux下ffmpeg安装
- JVM学习记录
- Linux 基本概念和操作
- JS 数组位置方法 indexOf()和lastIndexOf()的理解
- Scala学习(二)练习
- innodb next-key lock引发的死锁
- linux服务器---代理认证
- C#完美读取CSV
- C++STL学习笔记_(2)deque双端数组知识
- 算法笔记_118:算法集训之结果填空题集二(Java)
- shell脚本中,for基于列表进行循环的实现方法
- C#后台获取ajax传来的xml格式数据值