首页 > 技术文章 > 输出选中的下拉框的值

heyiming 2017-03-22 15:46 原文

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
        <select id="objsel" name="objsel" size=8 multiple>
            <option value="0" selected>请选择
            <option value="1">测试一
            <option value="2">测试二
            <option value="3">测试三
            <option value="4">测试四
            <option value="5">测试五
        </select>
        <input type="button" onclick="checkselect();" value="输出">
        选中的项目:<input type="text" id="output" name="output">
</body>
</html>
<script language="JavaScript">
    function checkselect(){
        var x = document.getElementById("objsel");
        var t = document.getElementById("output");
        var count=0;
        var intvalue="";
        for(i=0;i<x.length;i++){
            if(x.options[i].selected){
                intvalue+=x.options[i].value+",";
                count++;
            }
        }
        t.value=intvalue.substr(0,intvalue.length-1);
//        alert(count);
    }
</script>

效果图:

推荐阅读