使用JavaScript获取所选选项文本
我有这样一个下拉列表:使用JavaScript获取所选选项文本,javascript,html,dom,drop-down-menu,Javascript,Html,Dom,Drop Down Menu,我有这样一个下拉列表: <select id="box1"> <option value="98">dog</option> <option value="7122">cat</option> <option value="142">bird</option> </select> this.options[this.selectedIndex].text 狗 猫 鸟 如何使用JavaScrip
<select id="box1">
<option value="98">dog</option>
<option value="7122">cat</option>
<option value="142">bird</option>
</select>
this.options[this.selectedIndex].text
狗
猫
鸟
如何使用JavaScript获取实际选项文本而不是值?我可以通过以下方式获得值:
<select id="box1" onChange="myNewFunction(this.selectedIndex);" >
但是,您需要获取选项的innerHTML,而不是它的值,而不是7122
使用this.innerHTML
而不是this.selectedIndex
编辑:首先需要获取option元素,然后使用innerHTML
使用this.text
代替this.selectedIndex
您需要获取选项的innerHTML,而不是它的值
使用this.innerHTML
而不是this.selectedIndex
编辑:首先需要获取option元素,然后使用innerHTML
使用this.text
代替this.selectedIndex
使用-
$.trim($("select").children("option:selected").text()) //cat
这是小提琴-的用法-
$.trim($("select").children("option:selected").text()) //cat
这是小提琴-HTML:
演示:HTML:
演示:普通JavaScript
var sel = document.getElementById("box1");
var text= sel.options[sel.selectedIndex].text;
jQuery:
$("#box1 option:selected").text();
纯JavaScript
var sel = document.getElementById("box1");
var text= sel.options[sel.selectedIndex].text;
jQuery:
$("#box1 option:selected").text();
尝试选项
函数myNewFunction(sel){
警报(sel.options[sel.selectedIndex].text);
}
狗
猫
鸟
尝试选项
函数myNewFunction(sel){
警报(sel.options[sel.selectedIndex].text);
}
狗
猫
鸟
尝试以下方法:
myNewFunction = function(id, index) {
var selection = document.getElementById(id);
alert(selection.options[index].innerHTML);
};
请参见示例,尝试以下方法:
myNewFunction = function(id, index) {
var selection = document.getElementById(id);
alert(selection.options[index].innerHTML);
};
请参见示例所有这些函数和随机事件,我认为最好使用这些函数,并按如下方式操作:
<select id="box1">
<option value="98">dog</option>
<option value="7122">cat</option>
<option value="142">bird</option>
</select>
this.options[this.selectedIndex].text
所有这些函数和随机的东西,我认为最好使用这个,并这样做:
<select id="box1">
<option value="98">dog</option>
<option value="7122">cat</option>
<option value="142">bird</option>
</select>
this.options[this.selectedIndex].text
塞纳
联合国
双份
特洛伊
塞纳
联合国
双份
特洛伊
塞纳
联合国
双份
特洛伊
塞纳
联合国
双份
特洛伊
塞纳
联合国
双份
特洛伊
塞纳
联合国
双份
特洛伊
“严格使用”;
const s=document.querySelector(“.cS”);
//选项[this.selectedIndex].value
让fSel=(sIdx)=>console.log(sIdx,
s、 选项[sIdx].text,s.options[sIdx].textContent,s.options[sIdx].label);
设fSel2=(sIdx)=>{//this.value
console.log(sIdx,s.options[sIdx].text,
s、 选项[sIdx].textContent,s.options[sIdx].label);
}
//选项[this.selectedIndex].text
//选项[this.selectedIndex].textContent
//选项[this.selectedIndex].label
//选项[this.selectedIndex].innerHTML
设fSel3=(sIdx)=>{
console.log(sIdx);
}
//fSel
但是:
“严格使用”;
const x=document.querySelector(“.cS”),
o=x.选项,i=x.所选索引;
console.log(o[i].value,
o[i].text,o[i].textContent,o[i].label,o[i].innerHTML);
//.cS“
还有:
<select id="iSel" size="3">
<option value="one">Un</option>
<option value="two">Deux</option>
<option value="three">Trois</option>
</select>
<script type="text/javascript"> "use strict";
const i=document.getElementById("iSel");
for(let k=0;k<i.length;k++) {
if(k == i.selectedIndex) console.log("Selected ".repeat(3));
console.log(`${Object.entries(i.options)[k][1].value}`+
` => ` +
`${Object.entries(i.options)[k][1].innerHTML}`);
console.log(Object.values(i.options)[k].value ,
" => ",
Object.values(i.options)[k].innerHTML);
console.log("=".repeat(25));
}
</script>
联合国
双份
特洛伊
“严格使用”;
const i=document.getElementById(“iSel”);
for(设k=0;k`)+
`${Object.entries(i.options)[k][1].innerHTML}`);
console.log(Object.values(i.options)[k].value,
" => ",
值(i.options)[k].innerHTML);
console.log(“=”。重复(25));
}
塞纳
联合国
双份
特洛伊
塞纳
联合国
双份
特洛伊
塞纳
联合国
双份
特洛伊
塞纳
联合国
双份
特洛伊
塞纳
联合国
双份
特洛伊
塞纳
联合国
双份
特洛伊
“严格使用”;
const s=document.querySelector(“.cS”);
//选项[this.selectedIndex].value
让fSel=(sIdx)=>console.log(sIdx,
s、 选项[sIdx].text,s.options[sIdx].textContent,s.options[sIdx].label);
设fSel2=(sIdx)=>{//this.value
console.log(sIdx,s.options[sIdx].text,
s、 选项[sIdx].textContent,s.options[sIdx].label);
}
//选项[this.selectedIndex].text
//选项[this.selectedIndex].textContent
//选项[this.selectedIndex].label
//选项[this.selectedIndex].innerHTML
设fSel3=(sIdx)=>{
console.log(sIdx);
}
//fSel
但是:
“严格使用”;
const x=document.querySelector(“.cS”),
o=x.选项,i=x.所选索引;
console.log(o[i].value,
o[i].text,o[i].textContent,o[i].label,o[i].innerHTML);
//.cS“
还有:
<select id="iSel" size="3">
<option value="one">Un</option>
<option value="two">Deux</option>
<option value="three">Trois</option>
</select>
<script type="text/javascript"> "use strict";
const i=document.getElementById("iSel");
for(let k=0;k<i.length;k++) {
if(k == i.selectedIndex) console.log("Selected ".repeat(3));
console.log(`${Object.entries(i.options)[k][1].value}`+
` => ` +
`${Object.entries(i.options)[k][1].innerHTML}`);
console.log(Object.values(i.options)[k].value ,
" => ",
Object.values(i.options)[k].innerHTML);
console.log("=".repeat(25));
}
</script>
联合国
双份
特洛伊
“严格使用”;
const i=document.getElementById(“iSel”);
for(设k=0;k`)+
`${Object.entries(i.options)[k][1].innerHTML}`);
console.log(Object.values(i.options)[k].value,
" => ",
值(i.options)[k].innerHTML);
console.log(“=”。重复(25));
}
据我所知,有两种解决方案。
这两者都需要使用普通的javascript
1选择的选项
现场演示
const log=console.log;
constAreaSelect=document.querySelector(`[id=“area”]`);
areaSelect.addEventListener(`change`,(e)=>{
//日志(`e.target`,e.target);
const select=e.target;
常量值=select.value;
const desc=select.selectedOptions[0]。文本;
日志(`option desc`,desc);
});代码>
地区
A1
地下二层
C3
据我所知,有两种解决方案。
这两者都需要使用普通的javascript
1选择的选项
现场演示
const log=console.log;
constAreaSelect=document.querySelector(`[id=“area”]`);
areaSelect.addEventListener(`change`,(e)=>{
//日志(`e.target`,e.target);
const select=e.target;
常量值=select.value;
const desc=select.selectedOptions[0]。文本;
日志(`option desc`,desc);
});代码>
地区
A1
地下二层
C3
React/最新JavaScript
var sel = document.getElementById("box1");
var text= sel.options[sel.selectedIndex].text;
onChange={e=>e.currentTarget.option[e.selectedIndex].text}