JavaScript获取select下拉框中的第一个值

来源:互联网 发布:日本文化教育网络视频 编辑:程序博客网 时间:2024/04/28 10:13

JavaScript获取select下拉框中的第一个值


1、说明

      获取select下拉框中的第一个值


2、实现源码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>JavaScript获取select下拉框中的第一个值</title><script type="text/javascript">     /**  * JavaScript获取select下拉框中的第一个值  */     function getFirstValOfSelect() { //获取select中的ID var selectId = document.getElementById("select_option"); //获取select下拉框中第一个值 var selectValue = selectId.options[0].value; //获取select下拉框中第一个文本值 var selectText = selectId.options[0].text; //打印select下拉框中第一个值和文本值 alert("值:" + selectValue + "\n" + "文本值:" + selectText); }</script></head><body>   <div id="div_select">       <select id="select_option">            <option value="0">桃树</option>            <option value="1">梨树</option>            <option value="2">樟树</option>            <option value="3">枫树</option>            <option value="4">松树</option>            <option value="5">梧桐树</option>            <option value="6">槐树</option>       </select>   </div>   <input type="button" value="JavaScript获取select下拉框中的第一个值" onclick="getFirstValOfSelect()"/></body></html>

3、实现结果

(1)选中第一项



(2)选中第二项


1 0
原创粉丝点击