在this thread中,描述瞭如何使用JavaScript從下拉框中獲取選定的值。我一直試圖按照該線程中的說明進行操作,但一直未能實現。無法使用JavaScript在下拉框中獲取選定的值
這是我想要做的一個最小(非工作)的例子。代碼應該從下拉框中打印第二個選項的值,但是在第11行的Chrome的JavaScript控制檯Uncaught TypeError: Cannot read property 'options' of null
(即,當我定義第二個變量時)中出現以下錯誤。
<html>
<body>
<select name='a_drop_down_box'>
<option value='1'>One</option>
<option value='2' selected='selected'>Two</option>
<option value='3'>Three</option>
</select>
<p id='message'></p>
<script type="text/javascript">
var test = document.getElementById("a_drop_down_box");
var testValue = test.options[test.selectedIndex].value;
document.getElementById('message').innerHTML=testValue;
</script>
</body>
</html>