tec*_*tre 92 html javascript dom drop-down-menu
我有一个这样的下拉列表:
<select id="box1">
<option value="98">dog</option>
<option value="7122">cat</option>
<option value="142">bird</option>
</select>
Run Code Online (Sandbox Code Playgroud)
如何使用JavaScript获取实际选项文本而不是值?我可以通过以下方式获得价值:
<select id="box1" onChange="myNewFunction(this.selectedIndex);" >
Run Code Online (Sandbox Code Playgroud)
但不是7122我想要的cat.
999*_*99k 165
尝试选项
function myNewFunction(sel) {
alert(sel.options[sel.selectedIndex].text);
}Run Code Online (Sandbox Code Playgroud)
<select id="box1" onChange="myNewFunction(this);">
<option value="98">dog</option>
<option value="7122">cat</option>
<option value="142">bird</option>
</select>Run Code Online (Sandbox Code Playgroud)
sas*_*asi 67
简单的JavaScript
var sel = document.getElementById("box1");
var text= sel.options[sel.selectedIndex].text;
Run Code Online (Sandbox Code Playgroud)
jQuery的:
$("#box1 option:selected").text();
Run Code Online (Sandbox Code Playgroud)
kli*_*fia 11
所有这些功能和随机的东西,我认为最好使用它,并这样做:
this.options[this.selectedIndex].text
Run Code Online (Sandbox Code Playgroud)
两者都只需要使用 vanilla javascript
现场演示
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);
});Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
现场演示
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);
});Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
HTML:
<select id="box1" onChange="myNewFunction(this);">
Run Code Online (Sandbox Code Playgroud)
JavaScript的:
function myNewFunction(element) {
var text = element.options[element.selectedIndex].text;
// ...
}
Run Code Online (Sandbox Code Playgroud)
演示: http ://jsfiddle.net/6dkun/1/
用 -
$.trim($("select").children("option:selected").text()) //cat
Run Code Online (Sandbox Code Playgroud)
这是小提琴 - http://jsfiddle.net/eEGr3/
小智 5
使用普通 JavaScript
onChange = { e => e.currentTarget.options[e.selectedIndex].text }
Run Code Online (Sandbox Code Playgroud)
如果值在循环内,将为您提供准确的值。
小智 5
ECMAScript 6+
const select = document.querySelector("#box1");
const { text } = [...select.options].find((option) => option.selected);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
223892 次 |
| 最近记录: |