<html> <head> <script type="text/javascript"> function getIndex() { //从document对象中,获取select标签 var a=document.getElementById("mySelect"); //select标签获取的值其实是一个数组--a.options[]; 然后,选定项的下标是--a.selectedIndex var b=a.options[a.selectedIndex].value; alert(b); } </script> </head> <body> <form name="form1" id="form1"> Select your favorite fruit: <select id="mySelect" name="mySelect"> <option value="11">Apple</option> <option value="22">Orange</option> <option value="33">Pineapple</option> <option value="44">Banana</option> </select> <br/><br/> <input type="button" onclick="getIndex()" value="Alert index of selected option"> </form> </body> </html>
|