我有一个这样的下拉列表:

<select id="box1">
<option value="98">dog</option>
<option value="7122">cat</option>
<option value="142">bird</option>
</select>

我如何才能获得实际的选项文本,而不是使用JavaScript的值?我可以通过如下方式获取值:

<select id="box1" onChange="myNewFunction(this.selectedIndex);" >

但我想要猫而不是7122。


当前回答

ECMAScript 6 +。

const select = document.querySelector("#box1");

const { text } = [...select.options].find((option) => option.selected);

其他回答

HTML:

<select id="box1" onChange="myNewFunction(this);">

JavaScript:

function myNewFunction(element) {
    var text = element.options[element.selectedIndex].text;
    // ...
}

演示:http://jsfiddle.net/6dkun/1/

试着选择

myNewFunction(sel) { alert (sel.options [sel.selectedIndex]。text); } <select id="box1" onChange="myNewFunction(this);" > 狗<选项值= " 98 " > < /选项> 猫<选项值= " 7122 " > < /选项> 鸟<选项值= " 142 " > < /选项> < /选择>

使用- - - - - -

$.trim($("select").children("option:selected").text())   //cat

这里是小提琴- http://jsfiddle.net/eEGr3/

纯JavaScript

var sel = document.getElementById("box1");
var text= sel.options[sel.selectedIndex].text;

jQuery:

$("#box1 option:selected").text();

使用普通JavaScript

onChange = { e => e.currentTarget.options[e.selectedIndex].text }

如果值在循环中,会给你精确的值。