【问题标题】:jQuery Validation: How to not display errors? OR How to display errors as a tooltip?jQuery 验证:如何不显示错误?或如何将错误显示为工具提示?
【发布时间】:2010-09-23 12:00:35
【问题描述】:

我希望我的错误浮在左对齐的未验证输入字段上方。我该怎么做?

如果我不能,我该如何关闭错误?我仍然希望字段验证(并突出显示错误),但不希望显示实际的错误消息。我似乎无法在 jQuery 文档中找到任何可以让我打开/关闭它们的东西...... ??

【问题讨论】:

    标签: jquery validation


    【解决方案1】:

    按照 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;
    }
    

    【讨论】:

    • 对于 jQuery 位:应该使用 insertBefore() 而不是 before()。谢谢!
    【解决方案2】:

    您需要 errorPlacement 选项,可能还需要 errorContainer 和 errorElement 以进行进一步自定义。

    http://docs.jquery.com/Plugins/Validation/validate#toptions

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多