使用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

使用-

$.trim($("select").children("option:selected").text())   //cat
这是小提琴-

HTML:

演示:

普通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 = 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

塞纳
联合国
双份
特洛伊
塞纳
联合国
双份
特洛伊

塞纳 联合国 双份 特洛伊 塞纳 联合国 双份 特洛伊 塞纳 联合国 双份 特洛伊 塞纳 联合国 双份 特洛伊 “严格使用”; 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

React/最新JavaScript

var sel = document.getElementById("box1");
var text= sel.options[sel.selectedIndex].text;
onChange={e=>e.currentTarget.option[e.selectedIndex].text}

如果值在一个循环中,将给出精确的值。

我只是复制了所有amazon.com的“选择列表”,您可以从下面的image.gif链接中看到演示

我喜欢amazon.com的“选择/选项”css样式和javascript技巧

现在试试

/***javascript代码***/
document.querySelector(“#mySelect”).addEventListener(“单击”,()=>{
var x=document.querySelector(“mySelect”)。selectedIndex;
让optionText=document.getElementsByTagName(“选项”)[x].innerText;
document.querySelector(“.nav搜索标签”).innerText=optionText;
});
/***style.css***/
.导航左{
显示:-网络工具包盒;
显示器:-moz盒;
显示:-webkit flex;
显示:-ms flexbox;
显示器:flex;
位置:静态;
浮动:无;
}
.导航搜索范围{
显示:-网络工具包盒;
显示器:-moz盒;
显示:-webkit flex;
显示:-ms flexbox;
显示器:flex;
位置:相对位置;
浮动:无;
排名:0;
右:0;
底部:0;
左:0;
}
.nav搜索外观{
位置:相对位置;
浮动:左;
游标:默认值;
溢出:隐藏;
顶部:3px;
}
.导航搜索标签{
显示:块;
溢出:隐藏;
空白:nowrap;
文本溢出:省略号;
颜色:#555;
字体大小:12px;
线高:33px;
右边距:21px;
左边距:5px;
最小宽度:19px;
}
.导航图标{
位置:绝对位置;
顶部:14px;
右:8px;
边框样式:实心;
_边框样式:虚线;
边框宽度:4px;
边框颜色:透明;
边框顶部:4px实心#666;
边框底宽:0;
宽度:0;
身高:0;
字号:0;
线高:0;
}
.nav搜索下拉列表{
位置:绝对位置;
显示:块;
顶部:-1px;
左:0;
高度:35px;
宽度:自动;
字体家族:继承;
大纲:0;
保证金:0;
填充:0;
光标:指针;
不透明度:0;
过滤器:alpha(不透明度=0);
能见度:可见;
边界:0;
线高:35px;
}

全部的
各部门
工艺美术
汽车
宝贝
美容及个人护理
书
计算机
数字音乐
数码产品
Kindle商店
主要视频
女装
男装
女装
男装
交易
健康与家庭
家庭及厨房
工业和科学
行李
电影及电视
音乐、CD和乙烯基
宠物用品
软件
运动及户外
工具及家居装修
玩具及游戏
电子游戏
 <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 handleSelectChange: React.ChangeEventHandler<HTMLSelectElement> = (event) => {
    const {  options, selectedIndex } = event.target;
    const text = options[selectedIndex].text;
    // Do something...
  };