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>