1

我有一个复选框,我通过隐藏输入和定位嵌套在标签中的跨度来设置样式。见http://jsfiddle.net/rz6np/

HTML:

<input id="confirm" type="checkbox" name="confirm" value="1" required="required" />
<label for="confirm"><span>+</span>Confirm</label>

CSS:

    input[type="checkbox"] {
    display: none;
}

form input[type="checkbox"] + label span {
    display: inline-block;
    width: 16px;
    height: 16px;
    margin: 1px 10px 5px 0;
    vertical-align: middle;
    cursor: pointer;
    border: 1px solid grey;
    color: #fff;
    font-size: 15px;
    padding: 2px 2px;
}

input[type="checkbox"]:checked + label span {
    color: #000;
}

由于输入被隐藏,这意味着 html5required弹出窗口不显示。有没有办法强制它显示?

4

2 回答 2

0

假设设计看起来像这样或类似,不要使用 display: none。样式复选框足够大以覆盖它,因此只需将其放置在具有相对或绝对位置以及适当 z-index 的复选框上。

如果它不能完全覆盖它,您仍然可以在复选框上使用可见性:隐藏。即使该字段不可见,我仍然在 Firefox 中看到弹出窗口,但您需要检查其他浏览器以及它们的行为方式。

于 2013-04-30T21:38:28.363 回答
0

输入和跨度应该在标签内:

<label for="confirm">
   <input id="confirm" type="checkbox" name="confirm" value="1" required="required" />
   <span>+</span>
   Confirm
</label>

然后在输入:

label {
   position: relative;
}

label > input[type="checkbox"] {
   opacity: 0;
   position: absolute;
   top: 0;
   left: 0;
}
于 2019-12-11T03:52:59.753 回答