Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
444 views
in Technique[技术] by (71.8m points)

javascript - 使用JavaScript获取选定的选项文本(Get selected option text with JavaScript)

I have a dropdown list like this:(我有一个像这样的下拉列表:)

<select id="box1"> <option value="98">dog</option> <option value="7122">cat</option> <option value="142">bird</option> </select> How can I get the actual option text rather than the value using JavaScript?(如何使用JavaScript获取实际的选项文本而不是值?) I can get the value with something like:(我可以通过以下方式获得价值:) <select id="box1" onChange="myNewFunction(this.selectedIndex);" > But rather than 7122 I want cat .(但是我要的不是7122而是cat 。)   ask by techtheatre translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try options(尝试选项)

function myNewFunction(sel) { alert(sel.options[sel.selectedIndex].text); } <select id="box1" onChange="myNewFunction(this);"> <option value="98">dog</option> <option value="7122">cat</option> <option value="142">bird</option> </select>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...