问问题
8246 次
2 回答
3
你不能,<select>
是一个被替换的元素,它的子元素只作为它的数据而不是实际的子元素。
您能做的最好的事情就是将onChange
事件应用于<select>
自身,然后访问this.options[this.selectedIndex]
做事。
于 2013-02-01T17:42:49.000 回答
1
您将捕获 onchange 事件,然后测试以查看值更改为,如下所示:
更新:注意我添加的 onfocus 事件。
有关更多想法,请查看此处:HTML <select> 是否有 onSelect 事件或等效事件?
<script type="text/javascript">
function myDDLOnChangeEvent()
{
var selectedValue = document.getElementById('myDDL').value;
if(selectedValue == 'Foo')
{
//alert('Hi there');
//do stuff here - except for alerts
//if you put an alert here, it will fire the onfocus event again..
// ..after you click 'ok' in the alert box
}
}
</script>
<select id="myDDL" onchange="myDDLOnChangeEvent();" onfocus="this.selectedIndex = -1;">
<option>Foo</option>
<option>Bar</option>
</select>
于 2013-02-01T18:19:13.487 回答