单选按钮不工作单击

时间:2016-10-26 20:05:57

标签: html css forms radio-button

这是我的第一个问题所以请原谅我,如果我不清楚,问一个无知的问题或打破任何书面不成文的编程规则。我是一个非常初学的编码员,并承诺这不是我的意图!

我正在为我的公司制作一份简单的捐款表格。将有一组按钮,捐赠者可以从指定的预定金额中选择或手动输入他们自己的金额。我正在使用我设计的单选按钮看起来像块按钮。我无法使按钮保持检查状态(所选按钮应保持红色)。我已经尝试了所有可以考虑进行故障排除的事情,感觉我就在我开始的地方。非常感谢任何帮助!



.amount_list input {
  z-index: 999;
  visibility: hidden;
  position: absolute;
}
label {
  padding: 10px 20px;
  border: 1px solid #d50032;
  cursor: pointer;
  z-index: 90;
  border-radius: 5px;
  background: #fff;
  font-family: Arial, sans-serif;
  font-size: 14px;
  font-weight: bold;
  color: #d50032;
  display: inline-block;
  float: left;
  margin: 5px 10px 5px 0;
}
.amount_list {
  margin-left: auto;
  margin-right: auto;
  overflow: auto;
  float: left;
}
.gift_amount:hover {
  background: #d50032;
  color: #fff;
  border-radius: 5px;
  text-align: center;
  font-family: Arial, sans-serif;
}
.gift_amount + input:checked {
  -webkit-filter: none;
  -moz-filter: none;
  filter: none;
  background: #d50032;
  border: 1px solid #d50032;
  opacity: ;
  border-radius: 5px;
  text-align: center;
  font-family: Arial, sans-serif;
  color: #fff;
}
#inputother {
  border-radius: 5px;
  border: 1px solid #ccc;
  visibility: visible;
  padding: 12px 5px;
  margin: 0 5px;
}

<div class="amount_list">
  <label class="gift_amount">
    <input type="radio" value="1000" name="amount" />$1,000</label>
  <label class="gift_amount">
    <input type="radio" value="500" name="amount" />$500</label>
  <label class="gift_amount">
    <input type="radio" value="250" name="amount" />$250</label>
  <label class="gift_amount">
    <input type="radio" value="100" name="amount" />$100</label>
  <label class="gift_amount" for="other">
    <input type="radio" value="other" name="amount" id="other">Other</label>
  <input id="inputother" type="text" onchange="changeradioother()" />
</div>
&#13;
&#13;
&#13;

正如我所提到的,在编码方面我是一个新手,所以我可以从中学到任何错误,邋code的代码或不良习惯,请告诉我。谢谢!

2 个答案:

答案 0 :(得分:2)

您正在尝试为父元素设置样式,而无法使用CSS。

将价格换成<span>,然后改为样式:

&#13;
&#13;
.amount_list input {
  z-index: 999;
  visibility: hidden;
  position: absolute;
}
label span {
  padding: 10px 20px;
  border: 1px solid #d50032;
  cursor: pointer;
  z-index: 90;
  border-radius: 5px;
  background: #fff;
  font-family: Arial, sans-serif;
  font-size: 14px;
  font-weight: bold;
  color: #d50032;
  display: inline-block;
  float: left;
  margin: 5px 10px 5px 0;
}
.amount_list {
  margin-left: auto;
  margin-right: auto;
  overflow: auto;
  float: left;
}
.gift_amount:hover span {
  background: #d50032;
  color: #fff;
}
.gift_amount input:checked + span {
  background: #d50032;
  color: #fff;
}
&#13;
<div class="amount_list">
  <label class="gift_amount">
    <input type="radio" value="1000" name="amount" />
    <span>$1,000</span>
  </label>
  <label class="gift_amount">
    <input type="radio" value="500" name="amount" />
    <span>$500</span>
  </label>
  <label class="gift_amount">
    <input type="radio" value="250" name="amount" />
    <span>$250</span>
  </label>
  <label class="gift_amount">
    <input type="radio" value="100" name="amount" />
    <span>$100</span>
  </label>
  <label class="gift_amount" for="other">
    <input type="radio" value="other" name="amount" id="other">
    <span>Other</span>
  </label>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

要实现这一点,您必须更改标记+修正CSS规则,将input放在label之前并将CSS交换到

input:checked + .gift_amount {...}

还需要id以及for属性,以便将label与其input配对

样品

&#13;
&#13;
.amount_list input {
  z-index: 999;
  visibility: hidden;
  position: absolute;
}
label {
  padding: 10px 20px;
  border: 1px solid #d50032;
  cursor: pointer;
  z-index: 90;
  border-radius: 5px;
  background: #fff;
  font-family: Arial, sans-serif;
  font-size: 14px;
  font-weight: bold;
  color: #d50032;
  display: inline-block;
  float: left;
  margin: 5px 10px 5px 0;
}
.amount_list {
  margin-left: auto;
  margin-right: auto;
  overflow: auto;
  float: left;
}
.gift_amount:hover {
  background: #d50032;
  color: #fff;
  border-radius: 5px;
  text-align: center;
  font-family: Arial, sans-serif;
}
input:checked + .gift_amount {
  background: #d50032;
  border: 1px solid #d50032;
  opacity: ;
  border-radius: 5px;
  text-align: center;
  font-family: Arial, sans-serif;
  color: #fff;
}
#inputother {
  border-radius: 5px;
  border: 1px solid #ccc;
  visibility: visible;
  padding: 12px 5px;
  margin: 0 5px;
}
&#13;
<div class="amount_list">
  
  <input id="a1000" type="radio" value="1000" name="amount" />
  <label for="a1000" class="gift_amount">$1,000</label>

  <input id="a500" type="radio" value="500" name="amount" />
  <label for="a500" class="gift_amount">$500</label>

</div>
&#13;
&#13;
&#13;