如何在select标签上使用javascript获取id名称?
这是我的代码,当我测试它时会提醒undefined。
例如,当我在select标签中选择bbb时,我会提醒2
我该怎么做呢?
这是我的fiddle
<select id="test" onchange="test_fn()">
<option id="1" value="11">aaa</option>
<option id="2" value="22">bbb</option>
<option id="3" value="33">ccc</option>
</select>
<script type="text/javascript">
function test_fn()
{
alert(this.id);
}
</script>发布于 2016-04-14 22:19:57
实际上,您只需要select.options[select.selectedIndex].id,并使用this关键字将select元素作为参数传递给您的函数。
function test_fn(select)
{
console.log("ddd");
alert(select.options[select.selectedIndex].id);
}<select id="test" onchange="test_fn(this)">
<option id="1" value="11">aaa</option>
<option id="2" value="22">bbb</option>
<option id="3" value="33">ccc</option>
</select>
https://stackoverflow.com/questions/36625876
复制相似问题