【问题标题】:Not able to check a checkbox if is under styled label如果在样式标签下,则无法选中复选框
【发布时间】:2015-09-26 21:36:49
【问题描述】:

我正在尝试设置复选框输入的样式。我知道这是不可能的,因为浏览器。所以我给 input[type=checkbox] 赋予了隐藏的可见性,并且我用 FontAwesome 图标设置了标签的样式,当输入被选中时显示正常。我的问题是,如果我将输入放在标签的相同位置,则不会因为标签而检查输入。所以我必须把输入放在标签旁边,但这对用户来说不是什么好事。因此,如果有人对此有技巧或解决方案,那就太好了。

.log-in{
  background-color:$primary-color-opacity;
  padding: 7% 9% 7%;
  text-align: center;
  p{
    text-align:left;
  }
  input{    
    width:40%;
    height:40px;
    padding:1%;
  }
  input[type=checkbox]{
    opacity: 0;
    vertical-align: middle;
    width:20px;
    color:$error-color;
  }
  a{
    margin-top:3%;
  }
}

input[type=checkbox] + label::before {
  content: " ";
  position: absolute;
  left: 55px;
  top: 122px;
  width: 18px;
  height: 20px;
  display: block;
  background: white;
  border: none;
}

input[type=checkbox] + label::after {
  content: "\f00c";
  font-family: 'FontAwesome';
  color: $selector-color;
  position: absolute;
  left: 55px;
  top: 122px;
  width: 23px;
  height: 23px;
  display: block;
  z-index: 1;
  opacity: 0;
}

input[type=checkbox]:checked + label::after {
  opacity: 1;
}
<div class="log-in" >
  <p>Para poder participar debe aceptar las Condiciones particulares y el Plan de liquidación.</p>
  <div><input type="checkbox" ng-model="BscCtrl.conditions" ng-change="BscCtrl.checkConditions()">
  <label>Acepto las <a href="" class="color-link">Condiciones particulares</a> de la subasta</label>
  </div>
</div>

【问题讨论】:

    标签: javascript html css checkbox label


    【解决方案1】:

    给你的标签一个 for="" 属性对应于你的复选框输入的 id="" 属性 - 这样,当点击标签时,复选框将被选中。

    我在以下示例中使用“check1”作为值:

    <div class="log-in" >
      <p>Para poder participar debe aceptar las Condiciones particulares y el Plan de liquidación.</p>
      <div>
        <input id="check1" type="checkbox" ng-model="BscCtrl.conditions" ng-change="BscCtrl.checkConditions()">
        <label for="check1">Acepto las <a href="" class="color-link">Condiciones particulares</a> de la subasta</label>
      </div>
    </div>
    

    【讨论】:

    • 出色的煨设计。你完全正确。它现在工作完美。我喜欢这种无需更改太多代码的简单解决方案。非常感谢…
    【解决方案2】:

    /* custom checkboxes CSS */
        .custom-checkmark {
          position: relative;
        }
        
        .custom-checkmark input[type=checkbox]{
          opacity:0;  
        }
        
        .custom-checkmark label {
          margin-left: 25px;
        }
        
        .custom-checkmark input[type=checkbox]:checked+label:before,.custom-checkmark input[type=checkbox]:hover+label:before {
        	border-color:#369FF4;
        }
        
        .custom-checkmark input[type=checkbox]:checked+label:before {
          content: "\2713";
          line-height: 1.2;
        }
        
        .custom-checkmark input[type=checkbox]+label:before, .custom-checkmark input[type=radio]+label:before {
          content: "";
          display: inline-block;
          border: 1px solid #C9C9C9;
          /* font-family: 'cw-icons'; */
          background-color: #FFF;
          text-align: center;
          border-radius: 2px;
          color: #369FF4;
        }
        
        .custom-checkmark input[type=checkbox], .custom-checkmark label:before {
          position: absolute;
          width: 20px;
          height: 20px;
          left: 0;
          top: 0;
        }
    <div class="custom-checkmark">
      <input id="checkbox" type="checkbox">
      <label class="checkbox-label" for="checkbox">make this a recurring monthly gift</label>
    </div>

    【讨论】:

    • vinayaki 干得好。但是我只需要标签元素中的 for="" 来链接复选框,就像您在代码中所做的一样。我的复选框与此完美配合。谢谢!
    • ok.. 只是想分享自定义复选框 css,认为它与您的代码不同,也许我理解错了,您的 css 本身不起作用
    猜你喜欢
    • 1970-01-01
    • 2011-07-13
    • 1970-01-01
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多