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>
本文为原创文章,版权归国外主机测评所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ Linux 实用一键脚本,含 BBR、rclone/fclone/6pan-cli/aria2cli/youtube-dl、aria2、网盘挂载等等02/02
- ♥ MySQL 数据库查询字符串长度大于、小于或等于指定值的记录08/23
- ♥ DedeCMS 中 dede:channel 标签调用栏目简介 description 及关键词 keywords 的方法08/31
- ♥ ECShop 报错:Strict Standards: Only variables should be passed by reference in D:XXXincludescls_template.php on line 422 的原因及解决办法08/31
- ♥ 【疯狂猜成语/图猜成语】一根细绳拉起好几块石板是什么成语?08/21
- ♥ text-overflow 属性失效 text-overflow 在 li 下包含的 a 标签中无效08/25