在JavaScript中,获取`
<!DOCTYPE html>
<html>
<head>
<title>Select Example</title>
</head>
<body>
<select id="mySelect">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3" selected>Option 3</option>
<option value="4">Option 4</option>
</select>
<button onclick="getValueAndText()">Get Value and Text</button>
<script>
function getValueAndText() {
// 获取select元素
var selectElement = document.getElementById("mySelect");
// 获取选中的value
var value = selectElement.value;
// 获取选中的text,需要遍历options
var options = selectElement.options;
for (var i = 0; i < options.length; i++) {
if (options[i].value === value) {
var text = options[i].text;
break;
}
}
// 输出结果
alert("Value: " + value + ", Text: " + text);
}
</script>
</body>
</html>
在这个例子中,我们有一个`