html - 为什么输入类型复选框在选中时表现不同?

标签 html css checkbox styles

我的 input type = "checkbox"在选中时表现不同,它只适用于第一个 input type="checkbox"的一个元素,但是对于其他输入它工作不正确。

input[type="checkbox"] {
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    width: 16px;
    height: 16px;
    background-color: #fff;
    border: 1px solid #cccccc;
    cursor: pointer;
    transition: 0.3s;
}

input[type="checkbox"]:checked {
    background-color: #d9a83f;
}
<div class="first_body">
    <input type="checkbox" id="checkbox__id" class="checkbox regular_checkbox" value="value" />
    <label for="checkbox__id">Европейская</label>
</div>
<div class="last_body">
    <div class="last_body_child">
        <input type="checkbox" id="checkbox__id" class="checkbox regular_checkbox" value="value" />
        <label for="checkbox__id">Италия</label>
    </div>
    <div class="last_body_child">
        <input type="checkbox" class="checkbox regular_checkbox" />
        <label for="checkbox__id">Франция</label>
    </div>
</div>

当您单击其他输入时,即使您单击其他输入,也只会选中输入类型为“checkbox”的第一个元素。每个单独的输入都应单独突出显示。

我该如何解决这个问题?

最佳答案

您的 id for label-for 属性与第一个复选框匹配,因此它唯一选中/取消选中第一项。确保您的复选框 ID 与属性标签匹配。我想这就是你想要的:jsfiddle

<div class="first_body">
<input type="checkbox" id="checkbox__id" class="checkbox regular_checkbox" value="value" />
<label for="checkbox__id">Европейская</label>
</div>
<div class="last_body">
<div class="last_body_child">
    <input type="checkbox" id="checkbox__id_2" class="checkbox regular_checkbox" value="value" />
    <label for="checkbox__id_2">Италия</label>
</div>
<div class="last_body_child">
    <input type="checkbox" id="checkbox__id_3" class="checkbox regular_checkbox" />
        <label for="checkbox__id_3">Франция</label>
    </div>
</div>

关于html - 为什么输入类型复选框在选中时表现不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48374943/

相关文章:

php - 在 html 表单中键入输入字符需要匹配 - jQuery?

javascript - 文本输入结合下拉框

html 视频不在 iphone 上加载但在桌面网络和 android 上加载

jquery - 使用 jquery 放大时,图像在悬停时在 Google Chrome 中振动

javascript - 单击复选框时设置 bool 变量

android - 复选框在选中或未选中时显示不佳

c# - 从数据网格中获取复选框的值?

html - 在网页上使用排版引号是一种好习惯吗?

jquery - 悬停在其父 LI 上时如何触发链接悬停状态。查询

html - 如何使div中的背景图像响应