html+css 防止多次点击开关,并控制遍历中的数据判断开关按钮的数量

<style type="text/css">
    .chooseBtn {
        display: none;
    }
    .choose-label {
        box-shadow: #ccc 0px 0px 0px 1px;
        width: 40px;
        height: 20px;
        display: inline-block;
        border-radius: 20px;
        position: relative;
        background-color: #bdbdbd;
        overflow: hidden;
    }
    .choose-label:before {
        content: '';
        position: absolute;
        left: 0;
        width: 20px;
        height: 20px;
        display: inline-block;
        border-radius: 20px;
        background-color: #fff;
        z-index: 20;
        -webkit-transition: all 0.5s;
        transition: all 0.5s;
    }
    .chooseBtn:checked + label.choose-label:before {
        left: 20px;
    }
    .chooseBtn:checked + label.choose-label {
        background-color: #51ccee;
    }
</style>

<td class="am-text-middle">
    <input type="checkbox" name="sex" id="male.<?= $key ?>" class="chooseBtn "
        <?php if($first['is_publish'] == 1 ): ?>
            checked
        <?php endif; ?>
           onclick="choose_fun(<?=  $first['article_images_id'] ?>,<?= $key?>)" />
    <label for="male.<?= $key ?>" class="choose-label"></label>
</td>
<script>
    //更新数据
    var couldRun = true;
    function choose_fun(article_images_id,key) {
        if(couldRun){
            couldRun = false;
            //获取当前被选中数量
            var str = $(".chooseBtn:checked").length;
            var check = $(".chooseBtn").eq(key).is(":checked");
            if(check){
                if(str > 5) {
                    layer.msg('数量不可以大于5');
                    $(".chooseBtn").eq(key).removeAttr('checked');
                    return false;
                }
            }
            else{
                if(str < 1){
                    layer.msg('数量不可以小于1');
                    $(".chooseBtn").eq(key).prop('checked',true);
                    return false;
                }
            }
            $.ajax({
                url: "<?= url('content.article/imagesUpdate') ?>",
                type:"POST",
                dataType: 'text',
                data:{
                    article_images_id:article_images_id
                },
                dataType:"json",
                success: function(data){
                    if(data === 1){
                        layer.msg("修改成功!");
                    }else if(data === 2){
                        layer.msg("开启失败(显示数量已达上限!)");
                    }
                    else{
                        layer.msg("关闭失败(显示数量已达下限!)");
                    }
                }
            });
            setTimeout(function(){
                couldRun = true;
            },1000);
        }else{
            var check = $(".chooseBtn").eq(key).is(":checked");
            if(check){
                layer.msg('请勿连续点击!');
                $(".chooseBtn").eq(key).removeAttr('checked');
                return false;
            }else{
                layer.msg('请勿连续点击!');
                $(".chooseBtn").eq(key).prop('checked',true);
                return false;
            }
        }
    }
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值