Radiobutton只适用于一种形式而其他形式则不能

时间:2017-11-15 04:37:35

标签: css twitter-bootstrap forms radio-button

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

请帮帮我。提前致谢

ex: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>

2 个答案:

答案 0 :(得分:1)

Id必须是唯一的。所以请在无线电输入中给出唯一的id

&#13;
&#13;
[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;
}
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

如果我理解正确,你想要前两个无线电你需要设置不同的“searchFlights”表单一起工作,另外两个以“searchTrain”形式一起工作。

如果是这种情况,您需要在每种形式的无线电输入中使用不同的名称。无线电通过匹配输入的“名称”部分来工作。

您可以尝试更改每个集以使用其他名称。例如,对“searchFlights”表单使用“roundtripFlight”,对“searchTrains”表单使用“roundtripTrain”。

<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="roundtripFlight" id="oneway" value="oneway" checked="">
        <label for="oneway">Oneway</label>
      </li>
      <li id="radioFlights">
        <input type="radio" name="roundtripFlight" 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="roundtripTrain" id="oneway" value="oneway" checked="">
        <label for="oneway">Oneway</label>
      </li>
      <li id="radioTrains">
        <input type="radio" name="roundtripTrain" id="return" value="return">
        <label for="return">Return</label>
      </li>
    </ul>
  </form>
</div>