css - Radiobutton 仅适用于一种形式,其他形式不能

标签 css twitter-bootstrap forms radio-button

我正在尝试修改输入类型 radio 的输入样式,并且我有两种不同形式的 radio 输入。但 radio 输入仅适用于第一种形式,而第二种形式无效。我仍然对为什么不在第二个上工作这个问题感到困惑。

请帮帮我。提前致谢

例如:https://jsfiddle.net/devefrontend/vaqdsmjk/10/

<input type="radio" name="roundtrip" id="oneway" value="oneway" checked="">
<label for="oneway">Oneway</label>

[type="radio"]:checked,
[type="radio"]:not(:checked) {
  position: absolute;
  left: -9999px;
}

[type="radio"]:checked + label,
[type="radio"]:not(:checked) + label {
  position: relative;
  padding-left: 28px;
  cursor: pointer;
  line-height: 20px;
  display: inline-block;
  color: #666;
}

[type="radio"]:checked + label:before,
[type="radio"]:not(:checked) + label:before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  width: 18px;
  height: 18px;
  border: 1px solid #ddd;
  border-radius: 100%;
  background: #fff;
}

[type="radio"]:checked + label:after,
[type="radio"]:not(:checked) + label:after {
  content: '';
  width: 12px;
  height: 12px;
  background: #F87DA9;
  position: absolute;
  top: 4px;
  left: 4px;
  border-radius: 100%;
  -webkit-transition: all 0.2s ease;
  transition: all 0.2s ease;
}

[type="radio"]:not(:checked) + label:after {
  opacity: 0;
  -webkit-transform: scale(0);
  transform: scale(0);
}

[type="radio"]:checked + label:after {
  opacity: 1;
  -webkit-transform: scale(1);
  transform: scale(1);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<div role="tabpanel" class="tab-pane active" id="flights">
  <form name="flight" method="GET" action="" id="searchFlights" class="form-inline">
    <ul class="radio-list">
      <li id="radioFlights">
        <input type="radio" name="roundtrip" id="oneway" value="oneway" checked="">
        <label for="oneway">Oneway</label>
      </li>
      <li id="radioFlights">
        <input type="radio" name="roundtrip" id="return" value="return">
        <label for="return">Return</label>
      </li>
    </ul>
  </form>
</div>
<div role="tabpanel" class="tab-pane" id="trains">
  <form name="train" method="GET" action="" id="searchTrains" class="form-inline">
    <ul class="radio-list">
      <li id="radioTrains">
        <input type="radio" name="roundtrip" id="oneway" value="oneway" checked="">
        <label for="oneway">Oneway</label>
      </li>
      <li id="radioTrains">
        <input type="radio" name="roundtrip" id="return" value="return">
        <label for="return">Return</label>
      </li>
    </ul>
  </form>
</div>

最佳答案

Id 必须是唯一的。因此,请在 radio 输入中提供唯一的 id

[type="radio"]:checked,
[type="radio"]:not(:checked) {
  position: absolute;
  left: -9999px;
}

[type="radio"]:checked + label,
[type="radio"]:not(:checked) + label {
  position: relative;
  padding-left: 28px;
  cursor: pointer;
  line-height: 20px;
  display: inline-block;
  color: #666;
}

[type="radio"]:checked + label:before,
[type="radio"]:not(:checked) + label:before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  width: 18px;
  height: 18px;
  border: 1px solid #ddd;
  border-radius: 100%;
  background: #fff;
}

[type="radio"]:checked + label:after,
[type="radio"]:not(:checked) + label:after {
  content: '';
  width: 12px;
  height: 12px;
  background: #F87DA9;
  position: absolute;
  top: 3px;
  left: 3px;
  border-radius: 100%;
  -webkit-transition: all 0.2s ease;
  transition: all 0.2s ease;
}

[type="radio"]:not(:checked) + label:after {
  opacity: 0;
  -webkit-transform: scale(0);
  transform: scale(0);
}

[type="radio"]:checked + label:after {
  opacity: 1;
  -webkit-transform: scale(1);
  transform: scale(1);
}
ul{
  list-style:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<div role="tabpanel" class="tab-pane active" id="flights">
  <form name="flight" method="GET" action="" id="searchFlights" class="form-inline">
    <ul class="radio-list">
      <li id="radioFlights">
        <input type="radio" name="roundtrip" id="oneway" value="oneway" checked="">
        <label for="oneway">Oneway</label>
      </li>
      <li id="radioFlights">
        <input type="radio" name="roundtrip" id="return" value="return">
        <label for="return">Return</label>
      </li>
    </ul>
  </form>
</div>
<div role="tabpanel" class="tab-pane" id="trains">
  <form name="train" method="GET" action="" id="searchTrains" class="form-inline">
    <ul class="radio-list">
      <li id="radioTrains">
        <input type="radio" name="roundtrip" id="oneway2" value="oneway" checked="">
        <label for="oneway2">Oneway</label>
      </li>
      <li id="radioTrains">
        <input type="radio" name="roundtrip" id="return2" value="return">
        <label for="return2">Return</label>
      </li>
    </ul>
  </form>
</div>

关于css - Radiobutton 仅适用于一种形式,其他形式不能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47299436/

相关文章:

css - 使用 setCellStyleGenerator 时表 propertyId 值为 null

twitter-bootstrap - Twitter Bootstrap 模式外部 URL 不起作用

css - 有没有办法用 sass 或其他 css 模板语言进行模式匹配?

jquery - 使用 JQuery 修改 iFrame 中内容的 CSS

javascript - 单击按钮切换大小写

html - Bootstrap col-md 剪切标签的文字

javascript - 如何使用 Bootstrap 实现平滑滚动?

Javascript:如何使用循环函数在一个警告框中显示所有用户输入?

angularjs - Angular 1.2 : Is it possible to exclude an input on form dirty checking?

php - 使用 PHP 动态显示选项值