css自定义表单复选框

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>推送表单信息</title>
    <style>
        ul,li{
            list-style: none;
            display: inline-block;
            padding: .3rem
        }
        .box{width: 300px;height: 300px;border: 1px solid red;text-align: center}
        span{
            display: inline-block;
            vertical-align: middle;
            width: 2.4rem;
            text-align: left;
            overflow: hidden;
            white-space: nowrap;
        }
        ul li input{display: none}
        ul li input + label{
            background-color: white;
            border:1px solid #08cac2;
            width:.8rem;
            height:.8rem;
            display: inline-block;
            text-align: center;
            vertical-align: middle;
            line-height: .5rem;
            border-radius: 50%;
        }
        ul li input:checked + label{
            background-color: #eee;
        }
        ul li input:checked + label:after{
            content:"\2714";
            color: #08cac2;
        }
        input[type="button"]{
            width: 2.9rem;
            text-align: center;
            border: none;
            color: white;
            height: 1.5rem;
            line-height: 1.5rem;
            margin-top: 3.4rem;
            background-color: #96e2e2;
            border-radius: .2rem;
            box-shadow: 0px .2rem 1rem #ccf0f0;
        }
    </style>
</head>
<body>
    
    <form action="" id="box" class="box">
        <small>通讯录</small>
        <ul>
            <li>
                <span>张萌</span>
                <input type="checkbox" id="fx1">
                <label for="fx1"></label>
            </li>
            <li>
                <span>李子雯</span>
                <input type="checkbox" id="fx2">
                <label for="fx2"></label>
            </li>
            <li>
                <span>朱杰</span>
                <input type="checkbox" id="fx3">
                <label for="fx3"></label>
            </li>
            <li>
                <span>黄雨露</span>
                <input type="checkbox" id="fx4">
                <label for="fx4"></label>
            </li>
            <li>
                <span>柯惠</span>
                <input type="checkbox" id="fx5">
                <label for="fx5"></label>
            </li>
            <li>
                <span>徐倩</span>
                <input type="checkbox" id="fx6">
                <label for="fx6"></label>
            </li>
            <li>
                <span>王木木</span>
                <input type="checkbox" id="fx7">
                <label for="fx7"></label>
            </li>
            <li>
                <span>吴宇</span>
                <input type="checkbox" id="fx8">
                <label for="fx8"></label>
            </li>
            <li>
                <span>余宇萧</span>
                <input type="checkbox" id="fx9">
                <label for="fx9"></label>
            </li>
            <li>
                <span>王胖胖</span>
                <input type="checkbox" id="fx10">
                <label for="fx10"></label>
            </li>
            <li>
                <span>阮珍</span>
                <input type="checkbox" id="fx11">
                <label for="fx11"></label>
            </li>
            <li>
                <span>陈沐</span>
                <input type="checkbox" id="fx12">
                <label for="fx12"></label>
            </li>
        </ul>
        <input type="button" value="确认" onclick="sub()"/>
    </form>
    <h3 id="text">请选择接收人</h3>
</body>
</html>
<script>
    var inps = document.getElementsByTagName('input');
    var chooce = document.getElementById('text');
    for(var i=0;i<inps.length-1;i++){
        inps[i].index = i;
        inps[inps[i].index].onclick=function () {
            if(this.getAttribute('checked')==null){
                this.setAttribute('checked','checked')
            }else{
                this.setAttribute('checked',false);
            };
        }    
    }
    function sub() {
        // 最终显示文本
        var text='',txt=[];
        for(var i=0;i<inps.length-1;i++) {
            // 选中状态
            if (inps[inps[i].index].getAttribute('checked') == 'checked') {     
                text = inps[i].parentNode.getElementsByTagName('span')[0].innerText;
                txt.splice(i, 0, text.substr(inps[i].parentNode.getElementsByTagName('span')[0].innerText, 9));
            }else{
                inps[inps[i].index].removeAttribute('checked')
            }   
        }
        if(chooce.innerText==""){
            chooce.innerHTML = "请选择接收人";
        }else{
            chooce.innerHTML =txt.join(' ');
            if(chooce.innerHTML==""){
                chooce.innerHTML = "请选择接收人";
            }
        }
    }


</script>

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杀猪刀-墨林

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值