我有两个分别命名为日期和时间的下拉菜单。当我点击一个按钮时,我想从选项标签中获取 文本,但javascript功能似乎不起作用。我发现了类似的问题(和答案),但没有成功。代码如下:从html中的选择标记中获取文本
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function collectData() {
var index = document.getElementById("date").selectedIndex;
var date = document.getElementById("date").options[index].text;
window.alert("You selected: " + date);
}
</script>
</head>
<body>
<select name="date">
<option value="1">date 1</option>
.....
</select>
<select name="time">
<option value="1">time 1</option>
.....
</select>
<button type="button" onclick="collectData()">Get data</button>
</body>
</html>