首页 > 编程 > JavaScript > 正文

JavaScript实现获取select下拉框中第一个值的方法

2019-11-19 14:24: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)选中第二项

更多关于jQuery相关内容感兴趣的读者可查看本站专题:《jQuery表单(form)操作技巧总结》、《jQuery窗口操作技巧总结》、《jQuery常用插件及用法总结》、《jQuery表格(table)操作技巧汇总》、《jQuery扩展技巧总结》、《jQuery常见经典特效汇总》、《jQuery动画与特效用法总结》及《jquery选择器用法总结

希望本文所述对大家jQuery程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表