我需要获取文本而不是值,但我不知道如何。
示例代码:
<select id="a2">
<option value="b1">Burger Mcdo(Php 30.00)</option>
<option value="b2">Cheeseburger (Php 39.00)</option>
<option value="b3">Crispy Chicken Sandwich (Php 39.00)</option>
<option value="b4">Double Cheeseburger (Php 80.00)</option>
<option value="b5">Big Mac (Php 100.00)</option>
</select>
我可以获得选项值w /:
var z2 = document.getElementById('a2').value;
但我想得到文本值(例如Mac(Php 100.00))
答案 0 :(得分:1)
//get the select
var select = document.getElementById('a2');
console.log(select);
//set onchange handler
select.onchange= function(e){
//get selected index
var idx = select.selectedIndex;
//get content of selected option
var option = select.options[idx].textContent;
console.log(option);
};