css 改变radio、checkbox默认样式

<div class="input-item">
      <label for="sex">性别:</label>
      <div class="radio-wrap">
        <input type="radio" class="radio" value="0" id="sex" name="sex" checked="checked"/>
        <div class="label">√</div>
      </div>
      <span>男</span>
      <div class="radio-wrap">
        <input type="radio" class="radio" value="1" id="sex" name="sex"/>
        <div class="label">√</div>
      </div>
      <span>女</span>
    </div>
    
    <div class="input-item">
      <label for="fruit">水果:</label>
      <div class="checkbox-wrap">
        <input type="checkbox" class="checkbox" value="西瓜"/>
        <div class="label">√</div>
      </div>
      <span>西瓜</span>
      <div class="checkbox-wrap">
        <input type="checkbox" class="checkbox" value="菠萝" />
        <div class="label">√</div>
      </div>
      <span>菠萝</span>
    </div>
    
    <button type="button" onclick="toSubmit()">提交</button>
.input-item {
        display: flex;
        margin-top: 20px;
      }
      .radio-wrap {
        position: relative;
        margin-right: 10px;
      }
      .radio-wrap:nth-of-type(2) {
        margin-left: 30px;
      }
      .radio {
        width: 20px;
        height: 20px;
        margin: 0;
        opacity: 0;
        position: absolute;
        left: 0;
        top: 0;
      }
      .label {
        width: 20px;
        height: 20px;
        border-radius: 50%;
        text-align: center;
        border: 1px solid skyblue;
        color: skyblue;
      }
      input[type=radio]:checked + .label {
        background-color: skyblue;
        color: #fff;
      }
      
      .checkbox-wrap {
        position: relative;
        margin-right: 5px;
      }
      .checkbox-wrap .label {
        width: 20px;
        height: 20px;
        border-radius: 5px;
        color: skyblue;
        border: 1px solid skyblue;
      }
      .checkbox-wrap .checkbox {
        width: 20px;
        height: 20px;
        margin: 0;
        position: absolute;
        top: 0;
        left: 0;
        opacity: 0;
      }
      .checkbox-wrap .checkbox:checked + .label {
        background-color: skyblue;
        color: #FFFFFF;
      }
      .checkbox-wrap:nth-of-type(2) {
        margin-left: 30px;
      }
function toSubmit(){
      let radio = document.querySelectorAll('.radio');
      let sexVal = "0";
      for(let i=0; i<radio.length; i++){
        if(radio[i].checked){
          sexVal = radio[i].value;
        }
      }
      
      console.log(sexVal);
      
      
      let checkbox = document.querySelectorAll('.checkbox');
      let fruitArr = [];
      for(let i=0; i<checkbox.length; i++){
        if(checkbox[i].checked){
          fruitArr.push(checkbox[i].value)
        }
      }
      console.log(fruitArr);
      
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值