实现下拉菜单多选

这篇博客演示了如何使用HTML、CSS和JavaScript实现一个可多选的下拉菜单。通过循环遍历数组并生成HTML元素,创建了一个包含2015-2017年份的选项列表。当用户点击输入框时,下拉菜单显示,选择的年份将显示在输入框中。此外,当焦点离开输入框或下拉菜单时,菜单会自动隐藏。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>下拉多选</title>
    <script src="http://libs.baidu.com/jquery/1.11.1/jquery.min.js"></script>
    <style>
        ul li{
            list-style: none;
        }
        .hide{display: none}
        .width150{
            width: 150px;
        }
        .width150 input[type="text"]{
            width: 100%;
            height: 32px;
            border: 1px solid #ccc;
            border-radius: 4px;
            padding-left: 12px;
        }
        .width150 ul{
            width: 96%;
            padding: 0 0 0 20px;
            margin: 0;
            border: 1px solid #ccc;
        }
        .width150 li{
            padding: 5px;
        }
        .width150 li+li{
            border-top: 1px solid #ccc;
        }
    </style>
</head>
<body>
<form id="form">
    <div class="width150">
        可多选年份:<input type="text" id="yearInput" placeholder="请选择年份" readonly>
        <ul id="yearId" class="hide">
        </ul>
    </div>
</form>
<script>
    (function(){
        var str = '';
        var arr = {
            0 : {name:'2015',id:0},
            1 : {name:'2016',id:0},
            2 : {name:'2017',id:0}
        };
        for (let x in arr){
            console.info(x);
            str += `<li><label><input type="checkbox" value="${arr[x].id}" data-name="${arr[x].name}">${arr[x].name}</label></li>`;
        }
        $('#yearId').html(str);
    })();


    $("#yearInput").click(function(){
        $(this).attr('placeholder','');
        var name = '';
        $('#yearId input').each(function () {//循环遍历checkbox
            var  check=$(this).is(':checked');//判断是否选中
            if(check){
                name += $(this).attr('data-name')+',';
                $(this).attr('name',"yearId");
            }else {
                $(this).attr('name',"");
            }
        });
        $("#yearInput").val(name.slice(0,-1));//去除最后的逗号
    });


    $("#yearId").mouseover(function() {
        if (!$("#yearId").hasClass('hover')){//类hover在下面用来验证是否需要隐藏下拉,没有其他用途。
            $("#yearId").addClass('hover');
        }
    }).mouseout(function(){
        $("#yearId").removeClass('hover');
    });


    $(document).on('click',function() {
        if (!$("#yearInput").is(":focus") && !$("#yearId").hasClass('hover')) {//如果没有选中输入框且下拉不在hover状态。
            var name = '';
            $('#yearId input').each(function () {//遍历checkbox
                var check = $(this).is(':checked');//判断是否选中
                if (check) {
                    name += $(this).attr('data-name') + ',';
                    $(this).attr('name', "yearId");
                } else {
                    $(this).attr('name', "");
                }
            });
            $("#yearInput").val(name.slice(0, -1));//去除最后的逗号
            $("#yearId").addClass('hide');
        } else {
            $("#yearId").removeClass('hide');
        }
    });
</script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值