checkbook自定义多选,个数限制,数据回显

需求:弹框,多选(限制个数,5个),数据回显

实现:利用jq,自定义弹框,用checkbox自定义多选样式

图示

pic1为已选默认回显的数据,
pic2为点击输入框打开弹层显示可选项,已有选择需要回显已选项)


pic1.png
pic2.png

html

<div class="input_box">
    <input type="text" class="input_show" name="data" id="selectLable" value="" />
</div>
<div class="modal modal_labelSelect">
    <div class="modal_mask"></div>
    <div class="modal_content">
        <div class="modal_header">弹框标题(标签多选)</div>
        <div class="modal_body">
            <ul class="multipleSelectBox"></ul>
        </div>
        <div class="modal_footer">
            <button type="button" class="btn btn_cancel">取消</button>
            <button type="button" class="btn btn_save" id="saveMultiple">保存</button>
        </div>
    </div>
</div>

css

<style>
    *{padding: 0;margin:0;box-sizing: border-box;}
    html,body{
        height: 100%;
        font-size: 14px;
    }
    .input_box{
        width: 400px;
        height: 40px;
        margin: 20px auto;
    }
    .input_box input{
        width: 100%;
        height: 100%;
        text-indent: 1em;
        outline: none;
    }
    .modal_labelSelect{
        width: 100%;
        height: 100%;
        position: fixed;
        top: 0;
        left: 0;
        display: none;
    }
    .modal_mask{
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        background: rgba(20,20,20,.2);
    }
    .modal_header{
        height: 50px;
        line-height: 50px;
        position: relative;
    }
    .modal_header:after{
        content: '';
        width: 100%;
        height: 1px;
        background: #eee;
        position: absolute;
        bottom: 0;
        left: 0;
    }
    .modal_content{
        width: 500px;
        background: #fff;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%,-50%);
        border-radius: 20px;
        padding: 0 20px;
    }
    .modal_footer{
        display: flex;
        justify-content: center;
        align-items: center;
        padding: 20px 0;
        position: relative;
    }
    .modal_footer:before{
        content: '';
        width: 100%;
        height: 1px;
        background: #eee;
        position: absolute;
        top: 0;
        left: 0;
    }
    .modal_footer .btn{
        margin-right: 20px;
        width: 60px;
        height: 30px;
        display: flex;
        justify-content: center;
        align-items: center;
        border-radius: 6px;
        border: 1px solid #eee;
        outline: none;
        cursor: pointer;
        font-size: 16px;
    }
    .modal_footer .btn:last-of-type{
        margin-right: 0;
    }
    .modal_footer .btn.btn_save{
        background: #3591f4;
        color: #fff;
    }
    .multipleSelectBox {
        display: flex;
        flex-wrap: wrap;
    }

    .multipleSelectBox .itemSpan {
        list-style: none;
    }
    .multipleSelectBox .itemSpan input {
        display: none;
    }
    .multipleSelectBox .itemSpan label {
        display: block;
        width: 80px;
        border: 1px solid #ccc;
        border-radius: 6px;
        text-align: center;
        box-sizing: border-box;
        padding: 5px 10px;
        margin: 10px;
        cursor: pointer;
    }
    .multipleSelectBox .itemSpan label.active {
        border: 1px solid #3591f4;
        background: #3591f4;
        color: #fff;
    }
</style>

js

<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
    // 多选数据(字符串)
    var allLabel = 'label1,label2,label3,label4,label5,label6,label7,label8,label9,label10';
    // 页面打开显示的默认选择
    var defaultLabel = 'label2,label4';
    // 已选项
    var choiceArr = [];
    $(function(){
        var allLabelArray = allLabel.split(',');
        var defaultLabelArr = defaultLabel.split(',').join('/');
        $("#selectLable").val(defaultLabelArr);

        // 获取光标,打开多选弹框
        $("#selectLable").focus(function(){
            $(".modal_labelSelect").show();
            // 获取页面加载的默认选择项,动态添加获取的多选项
            var html = '';
            var selectLable = $('#selectLable').val();

            // 循环所有可选项添加到ul,并反显已选项
            for (var i in allLabelArray) {
                if (selectLable.indexOf(allLabelArray[i]) > -1) {
                    html += `<li class="itemSpan"><input type="checkbox" name="btncheckbox" id="${i}" value="${allLabelArray[i]}" checked/><label for="${i}" class="active">${allLabelArray[i]}</label></li>`
                } else {
                    html += `<li class="itemSpan"><input type="checkbox" name="btncheckbox" id="${i}" value="${allLabelArray[i]}"/><label for="${i}">${allLabelArray[i]}</label></li>`
                }
            }
            $(".modal_body .multipleSelectBox").html(html);
        });
        // 打开页面已选项 先清空再添加
        var selectLable = $('#selectLable').val();
        for (var i in allLabelArray) {
            if (selectLable.indexOf(allLabelArray[i]) > -1) {
                choiceArr.push(allLabelArray[i])
            }
        }
        console.log(choiceArr)

        // 多选 最多选择5项
        $(".multipleSelectBox").on("click", ".itemSpan label", function () {
            var inputAttr = $(this).siblings("input").prop('checked');
            if (!inputAttr) {
                choiceArr.push($(this).html());
                if (choiceArr.length <= 5) {
                    $(this).addClass("active");
                } else {
                    alert('最多选择5项!');
                    removeByValue(choiceArr, $(this).html());
                    $(this).removeClass("active");
                    $(this).siblings("input").prop('checked', function (i, val) {
                        return !val;
                    });
                }
            } else {
                removeByValue(choiceArr, $(this).html());
                $(this).removeClass("active");
            }

        });
        // 保存 并将已选项显示到输入框
        $('#saveMultiple').click(function () {
            var selectLable = "";
            $('#selectLable').val('');
            for (var i = 0; i < choiceArr.length; i++) {
                var item = choiceArr[i];
                selectLable = selectLable == ""?item:selectLable + "/" + item;
            }
            $(".modal_labelSelect").hide();
            $('#selectLable').val(selectLable);
        });
    })
    // 关闭弹窗
    $(".modal_mask,.btn_cancel").click(function(){
        $(".modal_labelSelect").hide();
    })
    // 移除选项,限制再选
    function removeByValue(arr, val) {
        for (var i = 0; i < arr.length; i++) {
            if (arr[i] == val) {
                arr.splice(i, 1);
                break;
            }
        }
    }
</script>

写在最后

反复测试或许还是会有bug,并不完备,最多5个的限制还有点问题,待努力,先就酱紫记录下来

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,651评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,468评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,931评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,218评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,234评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,198评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,084评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,926评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,341评论 1 311
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,563评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,731评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,430评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,036评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,676评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,829评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,743评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,629评论 2 354

推荐阅读更多精彩内容