0

我目前正在使用 jquery validate 插件版本 1.9.0 最新的是版本 1.11.0。当我迁移到最新版本时,表单的错误检查不起作用。在阅读了文档并查看了网站上的示例之后,我仍然无法找到解决方法。(有些例子不能正常工作)

在我的代码中,我使用元验证类来指定字段是否有效,并希望将其保留在 html 中,而不是在 JS 中指定它。

 $("#report-listing-form").validate({
     meta: "validate",
     rules: {
         recaptcha_response_field: {
             required: true
         }
     },
     errorContainer: {},
     invalidHandler: function (event, validator) {
         var errors = validator.numberOfInvalids();
         if (errors) {
             var message = (errors == 1)
                                    ? '1 invalid field.'
                                    : errors + ' invalid fields.';

             $("#err_report-listing-form").html(message).addClass("text-error");
         }
     },
     submitHandler: function (form) {
         alert('works');
     }
 });

我的代码示例在这里:

http://jsfiddle.net/deArQ/42/

为了使用旧插件的元数据必须使用这个插件

    /* ##################################################################################################### */
/* ##################################################################################################### */
/*
 * Metadata - jQuery plugin for parsing metadata from elements
 *
 * Copyright (c) 2006 John Resig, Yehuda Katz, J�örn Zaefferer, Paul McLanahan
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * Revision: $Id: jquery.metadata.js 4187 2007-12-16 17:15:27Z joern.zaefferer $
 *
 */

/**
 * Sets the type of metadata to use. Metadata is encoded in JSON, and each property
 * in the JSON will become a property of the element itself.
 *
 * There are three supported types of metadata storage:
 *
 *   attr:  Inside an attribute. The name parameter indicates *which* attribute.
 *          
 *   class: Inside the class attribute, wrapped in curly braces: { }
 *   
 *   elem:  Inside a child element (e.g. a script tag). The
 *          name parameter indicates *which* element.
 *          
 * The metadata for an element is loaded the first time the element is accessed via jQuery.
 *
 * As a result, you can define the metadata type, use $(expr) to load the metadata into the elements
 * matched by expr, then redefine the metadata type and run another $(expr) for other elements.
 * 
 * @name $.metadata.setType
 *
 * @example <p id="one" class="some_class {item_id: 1, item_label: 'Label'}">This is a p</p>
 * @before $.metadata.setType("class")
 * @after $("#one").metadata().item_id == 1; $("#one").metadata().item_label == "Label"
 * @desc Reads metadata from the class attribute
 * 
 * @example <p id="one" class="some_class" data="{item_id: 1, item_label: 'Label'}">This is a p</p>
 * @before $.metadata.setType("attr", "data")
 * @after $("#one").metadata().item_id == 1; $("#one").metadata().item_label == "Label"
 * @desc Reads metadata from a "data" attribute
 * 
 * @example <p id="one" class="some_class"><script>{item_id: 1, item_label: 'Label'}</script>This is a p</p>
 * @before $.metadata.setType("elem", "script")
 * @after $("#one").metadata().item_id == 1; $("#one").metadata().item_label == "Label"
 * @desc Reads metadata from a nested script element
 * 
 * @param String type The encoding type
 * @param String name The name of the attribute to be used to get metadata (optional)
 * @cat Plugins/Metadata
 * @descr Sets the type of encoding to be used when loading metadata for the first time
 * @type undefined
 * @see metadata()
 */

(function($) {

$.extend({
    metadata : {
        defaults : {
            type: 'class',
            name: 'metadata',
            cre: /({.*})/,
            single: 'metadata'
        },
        setType: function( type, name ){
            this.defaults.type = type;
            this.defaults.name = name;
        },
        get: function( elem, opts ){
            var settings = $.extend({},this.defaults,opts);
            // check for empty string in single property
            if ( !settings.single.length ) settings.single = 'metadata';

            var data = $.data(elem, settings.single);
            // returned cached data if it already exists
            if ( data ) return data;

            data = "{}";

            if ( settings.type == "class" ) {
                var m = settings.cre.exec( elem.className );
                if ( m )
                    data = m[1];
            } else if ( settings.type == "elem" ) {
                if( !elem.getElementsByTagName )
                    return undefined;
                var e = elem.getElementsByTagName(settings.name);
                if ( e.length )
                    data = $.trim(e[0].innerHTML);
            } else if ( elem.getAttribute != undefined ) {
                var attr = elem.getAttribute( settings.name );
                if ( attr )
                    data = attr;
            }

            if ( data.indexOf( '{' ) <0 )
            data = "{" + data + "}";

            data = eval("(" + data + ")");

            $.data( elem, settings.single, data );
            return data;
        }
    }
});

/**
 * Returns the metadata object for the first member of the jQuery object.
 *
 * @name metadata
 * @descr Returns element's metadata object
 * @param Object opts An object contianing settings to override the defaults
 * @type jQuery
 * @cat Plugins/Metadata
 */
$.fn.metadata = function( opts ){
    return $.metadata.get( this[0], opts );
};

})(jQuery);
4

1 回答 1

1

1) 拉出所有的内联规则。他们显然不会像这样工作......

class="{validate:{required:true, email: true}}"

.validate({...})而是在其中指定它们。不要忘记将任何包含特殊字符的名称放在引号内

rules: {
    'listing-report-email': {
        required: true,
        email: true
    }, 
    // your other rules
},

工作演示: http: //jsfiddle.net/4VP9X/4/ (运行 jQuery 1.9.1 和 JQuery Validate 1.11.0)


2) 规则maxWords要求包含additional-methods.js文件


3) 不要忘记将代码包装在 DOM 就绪事件处理函数中。

$(document).ready(function () {

    $("#report-listing-form").validate({
        // your rules & options
    });

});

4) 我也不知道该meta: "validate"选项应该为您做什么。 在我能找到的任何地方都没有记录它,而且没有它,您的代码似乎也能正常工作


5) errorContainer: {},

您已将errorContainer:选项留空。如果这是故意的,您可以完全忽略此选项。根据评论,将其替换为以下errorPlacement回调函数以抑制错误消息。然后为您的.valid.errorCSS 类设置样式以相应地突出显示有效和无效字段。

http://jsfiddle.net/4VP9X/8/

errorPlacement: function(error, element) {
    return false;  // will suppress all validation messages
},
于 2013-03-11T07:23:57.880 回答