直接上代码吧:

 

<html>
<body>

<button onClick="output()">test</button>

<select id="me">
   <option value="1">Volvo</option>
   <option value="2">Saab</option>
   <option value="3">Opel</option>
   <option value="4" selected="selected">Audi</option>
</select>

<script>
function output()
{
   var x = document.getElementById("me");
   alert(x.value);
}
</script>
  
</body>
</html>

       对用户来说, 显示的是Volvo, 但一旦被选择, 对程序员来说, 需要获取的是1这个值。 

 

       如上的selected表示默认选项。

 

       当然, 要在程序中捕捉用户的选项, 也可以这么搞, 用标签本书的属性, 如selectedIndex:

 

<html>
<body>

<button onClick="output()">test</button>

<select id="me">
   <option value="11">Volvo</option>
   <option value="22">Saab</option>
   <option value="33">Opel</option>
   <option value="44" selected="selected">Audi</option>
</select>

<script>
function output()
{
   var x = document.getElementById("me");
   alert(x.selectedIndex);
}
</script>
  
</body>
</html>

 

 

 

 

 

 

 

 


本文转载:CSDN博客