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

<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。


当前回答

使用jquery。 在你看来

  let selText = $("#box1 option:selected").text();
  console.log(selText);

其他回答

试着选择

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

纯JavaScript

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

jQuery:

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

使用jquery。 在你看来

  let selText = $("#box1 option:selected").text();
  console.log(selText);

所有这些函数和随机的东西,我认为最好使用这个,像这样做:

this.options[this.selectedIndex].text

试试下面的方法:

myNewFunction = function(id, index) {
    var selection = document.getElementById(id);
    alert(selection.options[index].innerHTML);
};

请看这里的jsfiddle示例