欢迎光临
我们一直在努力

js/jquery获取html select当前选中值

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

  <!doctype html>  <html>  <head>      <meta charset="UTF-8">      <title>js获取html select当前选中值-.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当前选中值-.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>
赞(0)
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《js/jquery获取html select当前选中值》
文章链接:https://www.ruiwuer.com/423.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。

登录

找回密码

注册