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>
本文为原创文章,版权归主机之家测评所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ git 提示:nothing added to commit but untracked files present 错误的解决办法08/27
- ♥ Laravel Passport invalid_client 错误的解决办法07/07
- ♥ Windows 下使用 git 时提示 Permission denied (publickey)的原因及解决办法08/24
- ♥ WPS 表格提取身份证号中的出生年月日并且自动计算年龄的方法08/29
- ♥ TP5 中 POST 接收数组出现:variable type error:array08/22
- ♥ 一个坐标上面有 NS 和辕辙是什么成语?08/17