Dragon
主机之家测评主机之家测评  2019-08-26 14:29 主机之家测评 隐藏边栏 |   抢沙发  82 
文章评分 0 次,平均分 0.0

js 获取 html select 当前选中值代码:

  <!doctype html>  <html>  <head>      <meta charset="UTF-8">      <title>js 获取 html select 当前选中值-www.02405.com</title>  </head>  <body>  <select id="user" onchange="showSelect()">      <option value="zhangsan">张三</option>      <option value="lisi">李四</option>      <option value="wangwu">王五</option>  </select>  <script type="text/javascript">      function showSelect(){          var obj = document.getElementById("user");          var index = obj.selectedIndex;//当前 select 选中项的索引          console.log(obj.options[index].value);//输出当前 select 选中项的 value          console.log(obj.options[index].text);//输出当前 select 选中项的 text      }  </script>  </body>  </html>

jquery 获取 html select 当前选中值代码:

  <!doctype html>  <html>  <head>      <meta charset="UTF-8">      <title>js 获取 html select 当前选中值-www.02405.com</title>      <script src="jquery.min.js"></script>  </head>  <body>  <select id="user" onchange="showSelect()">      <option value="zhangsan">张三</option>      <option value="lisi">李四</option>      <option value="wangwu">王五</option>  </select>  <script type="text/javascript">      function showSelect(){          console.log($("#user").find("option:selected").val());//输出当前 select 选中项的 value          console.log($("#user").find("option:selected").text());//输出当前 select 选中项的 text      }  </script>  </body>  </html>

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

发表评论

扫一扫二维码分享