【问题标题】:Displaying HTML5 validation message for custom checkbox显示自定义复选框的 HTML5 验证消息
【发布时间】:2016-10-06 13:58:33
【问题描述】:

我的注册表单使用 HTML5 验证来验证必填字段是否已完成。

<div class="container">
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <form action="https://www.salesforce.com" method="POST" id="signup">
                <div class="form-group">
                    <label for="first_name">First name</label>
                    <input id="first_name" maxlength="40" name="first_name" size="20" type="text" class="form-control" required>
                </div>
                <div class="checkbox">
                    <label>
                        <input id="tou" name="tou" type="checkbox" value="1" required="required"/>
                        I have read and agree to the terms of use.
                    </label>
                </div>
                <input type="submit" name="submit" class="btn">
            </form>
        </div>
    </div>
</div>

我已使用 CSS 将默认复选框替换为自定义 .svg 复选框。

input[type=checkbox] {
    display: inline-block;
    font-style: normal;
    font-weight: normal;
    line-height: 1;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    visibility: hidden;
}

input[type=checkbox]:before {
    content: url(/static/img/unchecked.svg);
    visibility: visible;
}

input[type=checkbox]:checked:before {
    content: url(/static/img/checked.svg);
}

虽然 HTML5 验证适用于其他字段,但我无法让它适用于自定义复选框。我尝试了jQuery validation with custom CSS checkbox中显示的方法,但没有成功。有什么想法吗?

【问题讨论】:

  • 如果您在进行 jquery 验证时遇到问题,为什么要向我们展示 css?
  • 是否有理由将 required 设置为属性?为什么不按照您在文本输入中设置所需的方式进行设置?
  • @AdamBuchananSmith 我想展示如何隐藏默认复选框,以防解决方案需要它。
  • @macksol 很好。没有理由将 required 设置为属性。

标签: css html forms checkbox twitter-bootstrap-3


【解决方案1】:

默认的 HTML5 验证错误消息是不可见的,因为您使用以下命令将其隐藏:

input[type=checkbox] {
    visibility: hidden;
}

试试这个:

input[type=checkbox] {
    opacity: 0;
}

在这里提琴:https://jsfiddle.net/xfosb27j/2/

【讨论】:

  • 感谢您的帮助 - 多年来您的回答对我很有帮助。
猜你喜欢
  • 2018-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-22
  • 2014-08-08
  • 2017-09-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多