• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
胡蝶forever
博客园    首页    新随笔    联系   管理    订阅  订阅
jquery实现简单的下拉多选

html布局:

<div class="mutiSelect">
    <span>街道列表</span>
    <div class="list">
        <p class="content">请选择</p>
    </div>
    <ul class="listContent">
        <li><input class="all" id="all" type="checkbox">全选</li>
        <li><input class="street" type="checkbox" name="street" value="1"> 街道1</li>
        <li><input class="street" type="checkbox" name="street" value="2"> 街道2</li>
        <li><input class="street" type="checkbox" name="street" value="3"> 街道3</li>
        <li><input class="street" type="checkbox" name="street" value="4"> 街道4</li>
        <li><input class="street" type="checkbox" name="street" value="5"> 街道5</li>
    </ul>
</div>

css样式

        *{padding:0;margin:0;}
        ul li,input{list-style: none;}
        .mutiSelect{width:50%;position: relative;margin:12px 0;text-indent: 3%;}
        .mutiSelect span{width:20%;height:25px;line-height:25px;display: block;position: absolute;top:0;font-size: 16px;color:#000;}
        .mutiSelect .list{width:30%;height:25px;line-height:20px;border:1px solid #a3bbc1;position: absolute;top:0;left:20%;}
        .mutiSelect .list p{width:100%;height:25px;line-height:25px;}
        .listContent{position: absolute;left:20%;width:30.2%;top:25px;display: none;}
        .listContent li{width:100%;border:1px solid #a3bbc1;box-sizing: border-box;border-top: none;line-height: 25px;}

jquery

<script>
    var flag=true;
    var num=0;
    var isc='';
    $(".content").click(function(){
        if(flag){
            flag=false;
            $(".listContent").show();
            isc=''
        }else{
            flag=true;
            $(".listContent").hide();
            for(var i=0;i<$(".street").length;i++){
                if($(".street").eq(i).is(':checked')){
                    isc +=$(".street").eq(i).val()+",";
                    num++;
                }
            };
            if(num>0){
                $(".content").text("街道/镇")
            }else{
                $(".content").text("请选择")
            }
            console.log(isc);

        }
    });


//     全选按钮设置
        $("#all").click(function(){
            if( $(this).is(':checked')){
                $(".street").prop("checked",true);
            }else{
                $(".street").prop("checked",false);
            }
        });

</script>

  

posted on 2017-04-21 16:02  胡蝶forever  阅读(837)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3