checkbox 修改样式+多选并取值

样式:

<script src="https://upcdn.b0.upaiyun.com/libs/jquery/jquery-2.0.2.min.js"></script>
 <script>
  $(function(){
    $('.checkAll').on('click',function(){
     var vm = $(this);
     /**全选**/
     if($('ul input').prop('checked')){
      $('ul input').prop('checked',true);
      $('ul').find('.mulit').addClass('mulitCur');
     }else{
      $('ul input').prop('checked',false);
      $('ul').find('.mulit').removeClass('mulitCur');
     }
    })
    /**
    1。点击菜名, 全选取消。 
    2。如果菜名全部选中,全选开始 。 (菜名的checked 和菜名的所有数是否相等,不相等全选取消)
    项不是全选的时候全选按钮取消选择 ,
    ***/
    $('ul').on('click',"input[name='veget']",function(){
     var vm = $(this);
     if(vm.prop('checked')){
      vm.parent().addClass('mulitCur');
     }else{
      vm.parent().removeClass('mulitCur');
     }
    

     var ischecked = $("ul input[name='veget']:checked").length;
     var inputAll = $("ul input[name='veget']").length;
     if(ischecked==inputAll){
      $('.checkAll input').prop('checked',true);
      $('ul').find('.mulit').addClass('mulitCur');
     // vm.parent()
      
     }else{
      $('.checkAll input').prop('checked',false);
      $('.checkAll .mulit').removeClass('mulitCur');
     }
    })

    //提交选中数据 (each循环出所有的ehecked ,多选放在数组里 ,转字符串用逗号隔开)
    $('.submit').on('click',function(){
     var temp =[];
     $('ul input[name=veget]:checked').each(function(val){
       temp[val]= $(this).val();
       })
     console.log(temp.join(',')); //需要的值
    });
     

  })
  </script>
<style>
  ul li{ list-style-type: none; float: left; margin: 5px;}
  ul input{
   outline: none; 
   font-size: 0;
   -webkit-appearance: none;
  }
  .mulit{
   cursor: pointer;
   background: #eee;
   color: #333;
   padding: 3px 5px;
  }
  .mulitCur{
   background: blue;
   color: #fff;
  }
  .submit{
   -webkit-appearance: none;
   line-height: 25px; 
   outline: none;
   width: 100px;
   margin-left: 20px;
  }
  
  /***
   1.这里是提交按钮样式
   2.样式丑 主要是记录。
  **/
  @-webkit-keyframes btn{
   0% { background: antiquewhite}
   100% {background: aquamarine;}
  }
  .submit:hover{
   -webkit-animation:btn 2s linear  ;
  }
 </style>
<ul>
 <li class="checkAll"><label class="mulit"><input type="checkbox">全选</label></li>
 <li><label class="mulit"><input type="checkbox" name="veget" value="白菜">白菜</label></li>
 <li><label class="mulit"><input type="checkbox" name="veget" value="芹菜">芹菜</label></li>
 <li><label class="mulit"><input type="checkbox" name="veget" value="酸菜">酸菜</label></li>
</ul>
<div>
 <button class="submit">提交</button>
</div>

记录一下

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值