如何使用 jquery 验证插件将错误消息和自定义图像添加到错误字段

发布于 2024-12-14 13:31:32 字数 326 浏览 1 评论 0原文

我正在使用 jquery 验证插件(我对它相当新),如果字段错误,我需要将错误消息与标签旁边的另一个自定义图像一起显示。

这就是我目前所拥有的:

messages: {
    fname: "Enter your firstname", 
        lname: "Enter your lastname"
},
errorPlacement: function(error, element) {
        error.appendTo( element.parent("td").next());

},

如何添加代码来附加图像和文本?

I'm using jquery validation plugin (which im fairly new to) and I need the error message to be displayed along with another custom image next to the label, if the field is in error.

This is what I have currently:

messages: {
    fname: "Enter your firstname", 
        lname: "Enter your lastname"
},
errorPlacement: function(error, element) {
        error.appendTo( element.parent("td").next());

},

how do i add code to also append an image with the text?

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

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

发布评论

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

评论(1

遮云壑 2024-12-21 13:31:32

这是 示例

以及这些示例的代码

// extend the current rules with new groovy ones

    // this one requires the text "buga", we define a default message, too
    $.validator.addMethod("buga", function(value) {
        return value == "buga";
    }, 'Please enter "buga"!');

    // this one requires the value to be the same as the first parameter
    $.validator.methods.equal = function(value, element, param) {
        return value == param;
    };

    $().ready(function() {
        var validator = $("#texttests").bind("invalid-form.validate", function() {
            $("#summary").html("Your form contains " + validator.numberOfInvalids() + " errors, see details below.");
        }).validate({
            debug: true,
            errorElement: "em",
            errorContainer: $("#warning, #summary"),
            errorPlacement: function(error, element) {
                error.appendTo( element.parent("td").next("td") );
            },
            success: function(label) {
                label.text("ok!").addClass("success");
            },
            rules: {
                number: {
                    required:true,
                    minlength:3,
                    maxlength:15,
                    number:true 
                },
                secret: "buga",
                math: {
                    equal: 11   
                }
            }
        });

    });

也是这一部分CSS的

em.error {
  background: url("images/unchecked.gif") no-repeat 0px 0px;
  padding-left: 16px;
}

Here is the example

And the code for those example

// extend the current rules with new groovy ones

    // this one requires the text "buga", we define a default message, too
    $.validator.addMethod("buga", function(value) {
        return value == "buga";
    }, 'Please enter "buga"!');

    // this one requires the value to be the same as the first parameter
    $.validator.methods.equal = function(value, element, param) {
        return value == param;
    };

    $().ready(function() {
        var validator = $("#texttests").bind("invalid-form.validate", function() {
            $("#summary").html("Your form contains " + validator.numberOfInvalids() + " errors, see details below.");
        }).validate({
            debug: true,
            errorElement: "em",
            errorContainer: $("#warning, #summary"),
            errorPlacement: function(error, element) {
                error.appendTo( element.parent("td").next("td") );
            },
            success: function(label) {
                label.text("ok!").addClass("success");
            },
            rules: {
                number: {
                    required:true,
                    minlength:3,
                    maxlength:15,
                    number:true 
                },
                secret: "buga",
                math: {
                    equal: 11   
                }
            }
        });

    });

Also the piece of CSS

em.error {
  background: url("images/unchecked.gif") no-repeat 0px 0px;
  padding-left: 16px;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文