CSS自定义复选框

时间:2019-02-05 17:34:03

标签: html css checkbox

我有一个带有30个复选框的页面。这些是座位预定。 现在,我尝试为它们设置样式。

.seatlayout{
  border: 1px solid;
  display: table;
  width: 30%;
  padding: 6% 2% 0% 2%;
  border-radius: 5%;
}
.seat{
  position: relative;
  width: 22%;
  margin-bottom: 10%;
  float: left;
  text-align: center;
  border: 1px solid #ccc;
}
[class*='seatcont'],
.seatdis{
  position: relative;
  width: 100%;
  height: 100%;
}

[class^="seatcon"] label,
.seatdis label{
  background-color: #f1f2ed;
  cursor: pointer;
  height: 100%;
  width: 100%;
  position: absolute;
  top: 0;
  left: 0;
}

[class^="seatcon"] input[type="checkbox"] ,
.seatdis input[type="checkbox"] {
  z-index:10

}

[class^="seatcon"] input[type="checkbox"]:checked + label
{
  background-color: #66bb6a;
}

.seatdis input[type="checkbox"]:checked + label{
  background-color: grey;
  pointer-events: none
}

[class^="seatcon"] input[type="checkbox"]:checked +  
label:after {
  opacity: 1;
}
<div class="seatlayout">
 <div class="seat">
    <div class="seatcont1">
       <input type="checkbox id="checkbox">
       <label for="checkbox" id="lab1">
    </div>
 </div>
 <div class="seat">
    <div class="seatcont2">
       <input type="checkbox id="checkbox">
       <label for="checkbox" id="lab2">
    </div>
 </div>
 <div class="seat">
    <div class="seatcont3">
       <input type="checkbox id="checkbox">
       <label for="checkbox" id="lab3">
    </div>
 </div>
</div>

  

问题   无论我在哪个座位上单击,它都会始终检查第一个座位。在使用通配符时,我尝试了几种添加唯一类和ID的方法。任何帮助将不胜感激!

3 个答案:

答案 0 :(得分:1)

您在所有复选框上都输入了相同的ID。您需要为他们提供不同的ID。 此外,您在类型之后的所有复选框上都缺少结尾引号。

<input type="checkbox" id="checkbox">
<label for="checkbox" id="lab1">

....

<input type="checkbox" id="checkbox2">
<label for="checkbox2" id="lab1">

答案 1 :(得分:1)

有很多错误。 label元素永远不会关闭。 label元素不需要id,它是for属性,需要与input的ID相匹配。其次,复选框的input永远不会被"关闭。第三,id始终需要唯一,这是唯一的identifier。因此,每个输入都需要有自己的唯一ID。

.seatlayout{
  border: 1px solid;
  display: table;
  width: 30%;
  padding: 6% 2% 0% 2%;
  border-radius: 5%;
}
.seat{
  position: relative;
  width: 22%;
  margin-bottom: 10%;
  float: left;
  text-align: center;
  border: 1px solid #ccc;
}
[class*='seatcont'],
.seatdis{
  position: relative;
  width: 100%;
  height: 100%;
}

[class^="seatcon"] label,
.seatdis label{
  background-color: #f1f2ed;
  cursor: pointer;
  height: 100%;
  width: 100%;
  position: absolute;
  top: 0;
  left: 0;
}

[class^="seatcon"] input[type="checkbox"] ,
.seatdis input[type="checkbox"] {
  z-index:10

}

[class^="seatcon"] input[type="checkbox"]:checked + label
{
  background-color: #66bb6a;
}

.seatdis input[type="checkbox"]:checked + label{
  background-color: grey;
  pointer-events: none
}

[class^="seatcon"] input[type="checkbox"]:checked +  
label:after {
  opacity: 1;
}
<div class="seatlayout">
 <div class="seat">
    <div class="seatcont1">
       <input type="checkbox" id="checkbox1">
       <label for="checkbox1"> 1</label>
    </div>
 </div>
 <div class="seat">
    <div class="seatcont2">
       <input type="checkbox" id="checkbox2">
       <label for="checkbox2"> 2</label>
    </div>
 </div>
 <div class="seat">
    <div class="seatcont3">
       <input type="checkbox" id="checkbox3">
       <label for="checkbox3"> 3</label>
    </div>
 </div>
</div>

答案 2 :(得分:0)

每个复选框的ID相同。您应该为每个ID使用唯一的ID。

相关问题