更改select下拉菜单项获取对应的值和文本:
在实际应用中,更改select下拉菜单项就去执行一段代码有着大量的应用,例如更改下拉菜单项实现页面跳转等,下面介绍一下如何更改select下拉菜单项获取当前选中的option项的value值和文本内容。
代码如下:
<!DOCTYPE html><html><head><meta charset=" utf-8"><meta name="author" content="http://www.softwhy.com/" /><title>蚂蚁部落</title><script type="text/javascript">function selChange(){ var sel=document.getElementById('sel'); alert(sel.options[sel.selectedIndex].text); alert(sel.options[sel.selectedIndex].value);}window.onload=function(){ var sel=document.getElementById("sel"); sel.onchange=selChange;}</script></head><body><select id="sel"><option value="1">蚂蚁部落一</option><option value="2">蚂蚁部落二</option><option value="3">蚂蚁部落三</option></select></body></html>
以上代码中,更改select下拉菜单项能够弹出当前选中项的value值和text文本值。
相关阅读:
1.selectedIndex属性可以参阅javascript的select.selectedIndex属性一章节。
2.onchange事件可以参阅javascript的select的onchange事件一章节。
3.select相关操作可以参阅javascript动态操作select下拉列表一章节。
原文地址是:http://www.softwhy.com/forum.php?mod=viewthread&tid=11648
更多内容可以参阅:http://www.softwhy.com/javascript/
原标题:更改select下拉菜单项获取对应的值和文本
关键词: