Javascript 如何获得显示在下拉列表中的值而不是它的值?

Javascript 如何获得显示在下拉列表中的值而不是它的值?,javascript,jquery,html,Javascript,Jquery,Html,我希望使用javascript在下拉列表中获取值,而不是值 比如说 <select id="mySelect" onchange="myFunction()"> <option value="1">Audi</option> <option value="2">BMW</option> <option value="3">Mercedes</option> <option value="4">Volv

我希望使用javascript在下拉列表中获取值,而不是值 比如说

<select id="mySelect" onchange="myFunction()">
<option value="1">Audi</option>
<option value="2">BMW</option>
<option value="3">Mercedes</option>
<option value="4">Volvo</option>
</select>
当我选择奥迪时,我希望获得值奥迪,而不是值1

<script>
function myFunction() {
var x = document.getElementById("mySelect").value;
document.getElementById("demo").innerHTML = "You selected: " + x;
}
</script>

使用jQuery,您可以这样做。收听下拉列表的更改事件,并获取所选选项的内部文本

$mySelect.changefunction{ console.log$this.findoption:selected.text; }; 奥迪 宝马 梅赛德斯 沃尔沃汽车
使用jQuery,您可以这样做。收听下拉列表的更改事件,并获取所选选项的内部文本

$mySelect.changefunction{ console.log$this.findoption:selected.text; }; 奥迪 宝马 梅赛德斯 沃尔沃汽车
您必须从所选选项中选取文本:

使用香草JavaScript:

函数myFunctionel{ var text=el.options[el.selectedIndex].textContent document.getElementByIddemo.textContent=您选择的:+text; } myFunctiondocument.getElementByIdmySelect//为默认选定选项调用页面加载功能 奥迪 宝马 梅赛德斯 沃尔沃汽车
您必须从所选选项中选取文本:

使用香草JavaScript:

函数myFunctionel{ var text=el.options[el.selectedIndex].textContent document.getElementByIddemo.textContent=您选择的:+text; } myFunctiondocument.getElementByIdmySelect//为默认选定选项调用页面加载功能 奥迪 宝马 梅赛德斯 沃尔沃汽车 您可以使用value&selectedIndex&textContent获取值和选项文本

函数myFunctionelem{ 设k=elem.options[elem.selectedIndex].textContent; console.logelem.value,k } 奥迪 宝马 梅赛德斯 沃尔沃汽车 您可以使用value&selectedIndex&textContent获取值和选项文本

函数myFunctionelem{ 设k=elem.options[elem.selectedIndex].textContent; console.logelem.value,k } 奥迪 宝马 梅赛德斯 沃尔沃汽车 试试这个:

document.querySelector'mySelect'。addEventListener'input',函数{ console.logthis.options[this.selectedIndex].innerHTML; };

试试这个:

document.querySelector'mySelect'。addEventListener'input',函数{ console.logthis.options[this.selectedIndex].innerHTML;
};

您需要获取文本,而不是值。请尝试下面的代码-

函数myFunctionelem{ console.logelem.options[elem.selectedIndex].text; } 奥迪 宝马 梅赛德斯 沃尔沃汽车
您需要获取文本,而不是值,请尝试下面的代码-

函数myFunctionelem{ console.logelem.options[elem.selectedIndex].text; } 奥迪 宝马 梅赛德斯 沃尔沃汽车 您可以尝试以下代码:

函数myFunction{

var selectedData=$this.find'option:selected'.html

}

您可以尝试以下代码:

函数myFunction{

var selectedData=$this.find'option:selected'.html


}

您可以选择值为1的选项标记并获取其innertextfunction myFunction{alert$'mySelect option:selected'.text}您可以选择值为1的选项标记并获取其innertextfunction myFunction{alert$'mySelect option:selected'.text}所有的代码都是正确的,但是这个代码感觉像是一个简洁明了的代码。这就是为什么我选择这个作为正确答案的原因。谢谢所有的代码都是正确的,但这段代码感觉像是一段简洁明了的代码。这就是为什么我选择这段代码作为正确答案的原因。谢谢