jQuery验证:如何不显示错误?或如何将错误显示为工具提示?

11

我希望我的错误信息能够左对齐地浮现在验证失败的输入框上方。我该怎么做呢?

如果不能实现,那我该如何关闭这些错误信息呢?我仍然希望字段进行验证(并在出错时高亮显示),但不想显示实际的错误消息。在 jQuery 文档中似乎找不到任何可以打开或关闭这些功能的方法...??

2个回答

14

按照J Cooper的建议,在您的jQuery验证调用中使用errorPlacement属性:

$(...).validate({
    errorPlacement: function(error, element) {
        error.insertBefore(element);
    }
});

还有 CSS 来为错误样式化(在你的样式表中,或者针对个别元素):

label.error {
    /* Move the error above the input element. */
    position: absolute;
    line-height: 1.5em;
    margin-top: -1.5em;

    background-color: red;
    color: white;
    padding: 0 2px;
}

2
对于jQuery的部分:应该使用insertBefore()而不是before()。谢谢! - neezer

11

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接