jQuery验证errorPlacement [英] jQuery Validation errorPlacement

查看:87
本文介绍了jQuery验证errorPlacement的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有效:

$("[id$='_zzz']").rules(
    "add",
    {
        required: true,
        minlength: 8,
        messages: {
            required: "...",
            minlength: jQuery.format("...")
        }            
    }
);

出现错误消息.

当我尝试设置消息的样式时,这不起作用:

$("[id$='_zzz']").rules(
    "add",
    {
        required: true,
        minlength: 8,
        messages: {
            required: "...",
            minlength: jQuery.format("...")
        },
        errorElement: "span",
        errorPlacement: function(error, element) {
            error.insertAfter(element);
            error.css("margin", "0 0 0 5px");
        }             
    }
);

当我通过validate函数设置样式时,将应用样式以使其起作用:

$('#aspnetForm').validate({
    errorElement: "span",
    errorPlacement: function(error, element) {
        error.insertAfter(element);
        error.css("margin", "0 0 0 5px");
    }
});

为什么不能在规则添加功能中使用错误位置进行样式设置?

解决方案

在第一种方法中,您将对象上的errorPlacement属性/函数传递给rules方法...它根本不在乎检查并不使用它.对于您传入javascript的任何对象,它都必须检查该属性或以某种方式使用它,否则,它只是无关的信息.您也可以将myExtrathing:'ValueHere'添加到对象中,不会破坏任何内容……但也不会使用.

.validate()方法具有现有的errorPlacement函数(这是积极使用的,这是很重要的部分,它在默认选项中为查找),并且 看到一些简单的示例.. >

This works:

$("[id$='_zzz']").rules(
    "add",
    {
        required: true,
        minlength: 8,
        messages: {
            required: "...",
            minlength: jQuery.format("...")
        }            
    }
);

The error message comes up.

When I try to style the message, this doesn't work:

$("[id$='_zzz']").rules(
    "add",
    {
        required: true,
        minlength: 8,
        messages: {
            required: "...",
            minlength: jQuery.format("...")
        },
        errorElement: "span",
        errorPlacement: function(error, element) {
            error.insertAfter(element);
            error.css("margin", "0 0 0 5px");
        }             
    }
);

When I style via the validate function, the styling is applied so it works:

$('#aspnetForm').validate({
    errorElement: "span",
    errorPlacement: function(error, element) {
        error.insertAfter(element);
        error.css("margin", "0 0 0 5px");
    }
});

Why can't I style using errorplacement in the rules add function?

解决方案

In the first method, you're passing a errorPlacement property/function on the object to the rules method...it simply doesn't care of check for this and doesn't use it. For any object you pass in to javascript it has to check for that property or make use of it in some manner, otherwise it's just extraneous information. You could add myExtrathing:'ValueHere' to the object as well, wouldn't break anything...but wouldn't be used either.

The .validate() method has an existing errorPlacement function (that is actively uses, that's the important part, it's looking for it) in the default options and performs an extend to replace the default method with the one your provided...if you provided one. This same behavior is true for almost every jQuery plugin and any option you specify, it merges the default options with the ones you provide/override. You can see some simple examples of this behavior in the jQuery plugin authoring manual.

这篇关于jQuery验证errorPlacement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆