这些事件在选择中起作用,而不是选项,而只是脚本的条件。
<html>
<body>
<script>
function alertMe(option){
if(option == "saab"){
window.alert("you clicked saab in selec!");
document.getElementById("txt").value='saab!';
}
}
</script>
<select onChange="alertMe(this.value);">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="m"> Mercedes</option>
<option>Audi</option>
</select>
<input type="text" id="txt" value="change me"></input>
</body>
</html>
[更新的答案]
- 您可以使用
document.getElementById('topic').options.selectedIndex
or访问选择mySelect.options[selectedIndex].value
(不适用于我)。显然你可以创建一个你可以参考的表格,document.forms['topic'].elements[id]
但不确定这个表格。更多的。
- @Kosh 这很烦人!只需执行 a
dictionary = {0:"topic", 1:"hello",...,n:"topic_{n}"}
然后将其getElementById('topic').selectedId
用作键并忘记值。未解决,但已四舍五入。
[更新]我想我解决了,只是el.value
,selectedIndex
不需要:
<html>
<body>
<script>
function printSelectValue()
{
var el = document.getElementById("selectOne");
alert(el.value);
}
</script>
<select id="selectOne" onChange="printSelectValue()">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="m"> Mercedes</option>
</select>
</body>
</html>