【问题标题】:Custom radio button is not selecting on click自定义单选按钮未在单击时选择
【发布时间】:2018-09-07 00:52:25
【问题描述】:

/* radio buttons */
 .radio-container {
	 display: block;
	 position: relative;
	 cursor: pointer;
	 -webkit-user-select: none;
	 -moz-user-select: none;
	 -ms-user-select: none;
	 user-select: none;
	 padding-left: 1.5em;
	 margin-bottom: 0.75em;
}
 .radio-container input {
	 position: absolute;
	 opacity: 0;
	 cursor: pointer;
}
 .radio-container input:checked .radio:after {
	 display: block;
}
 .radio-container:hover .radio {
	 background: gray;
}
/* custom radio button */
 .radio {
	 position: absolute;
	 top: 0;
	 left: 0;
	 height: 1em;
	 width: 1em;
	 background-color: transparent;
	 border: 1px solid gray;
	 border-radius: 50%;
}
 .radio:after {
	 content: "";
	 position: absolute;
	 display: none;
	 top: 0;
	 left: 0;
	 width: 0.25em;
	 height: 0.25em;
	 border-radius: 50%;
	 background: white;
}
 
<form class="recharge">
  <div>
  <label class="radio-container" for="subscribe">
  <input type="radio" id="one-time" name="recharge">
  <span class="radio"></span>
  Subscribe & Save 10%
  </label>
  </div>
  <div>
  <label class="radio-container" for="one-time">
  <input type="radio" id="one-time" name="recharge">
  <span class="radio"></span>
  One Time Purchase
  </label>
  </div>
</form>

我在我的网站上为单选按钮添加了自定义样式,以赋予它们自定义样式。我的 HTML 和 CSS 代码附在上面的 sn-p 中。但是,现在当我单击输入时,它不会选择。理想情况下,我希望在没有 JS 组件的情况下也能正常工作。

【问题讨论】:

    标签: html css forms radio-button


    【解决方案1】:

    请找到您的问题的解决方案:

    Codepen link to the solution

    我发现的一个问题是,单击单选按钮后您没有指定颜色,并且兄弟选择器也丢失了。我专门添加了这些行:

    .radio-container input:checked ~ .radio {
    background-color: #2196F3;
    }
    

    希望对你有帮助!!谢谢。

    【讨论】:

    猜你喜欢
    • 2014-08-30
    • 2012-01-12
    • 2012-12-13
    • 1970-01-01
    • 2020-07-21
    • 2013-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多