JQuery 验证:如何将错误消息放置在输入的标题属性中

发布于 2024-08-25 12:04:55 字数 458 浏览 3 评论 0原文

由于空间限制,我尝试将表单的错误消息放置在工具提示中。

$("form").validate({
        rules: {
            username: { required: true, email: true },
            password: "required"
        },
        onkeyup: true,
        success: "valid",
        errorPlacement: function(error, element) {
            var msg = errorList[i].message;
            element.attr('title', msg);
        }
    });

element.attr 部分工作正常 - 我只是在提取要放入其中的消息时遇到问题。它驻留在哪里?

Due to space limitations I'm trying to place the error messages for a form inside tooltips.

$("form").validate({
        rules: {
            username: { required: true, email: true },
            password: "required"
        },
        onkeyup: true,
        success: "valid",
        errorPlacement: function(error, element) {
            var msg = errorList[i].message;
            element.attr('title', msg);
        }
    });

The element.attr part works fine- I'm just having trouble extracting the message to put in it. Where does it reside?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜宝宝 2024-09-01 12:04:55

您的代码有几个问题

  • errorList 未定义
  • i 未定义,

但我猜这应该可以正常工作(尽管我不知道插件如何反应,因为它可能期望在此回调之后要插入到 DOM 中的错误元素)

errorPlacement: function(error, element) {
    element.attr('title', error.text());
    //or if that doesn't work try also 
    //element.attr('title', error[0].text);
}

There are several issues with your code

  • errorList is undefined
  • i is undefined

but this should just work fine I guess (although I don't know how the plugin reacts as it probably expects the error element to be inserted into the DOM after this callbacK)

errorPlacement: function(error, element) {
    element.attr('title', error.text());
    //or if that doesn't work try also 
    //element.attr('title', error[0].text);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文