当前位置: 主机百科 » 资源 » 技术 » 正文

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

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

1
  <!doctype html>  <html>  <head>      <meta charset="UTF8">      <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      }  ipt>    

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

1
  <!doctype html>  <html>  <head>      <meta charset="UTF8">      <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      }  ipt>    

未经允许不得转载:主机百科 » js/jquery获取html select当前选中值

相关文章