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

admin 阅读:132 2024-03-03

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


<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>js获取html select当前选中值-www.codesou.cn</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.codesou.cn</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>
声明

1、部分文章来源于网络,仅作为参考。
2、如果网站中图片和文字侵犯了您的版权,请联系1943759704@qq.com处理!