验证插件-自动验证页面上的多种形式 [英] Validation plugin - Auto validate multiple forms on a page

查看:68
本文介绍了验证插件-自动验证页面上的多种形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我需要验证每个表单,如下所示:

Currently I need to validate every form like this:

    $(document).ready(function () {
        $('#admin_settings_general').validate({
            rules: {
                admin_settings_application_title: {
                    required: true
                }
            },
            highlight: function (element) {
                $(element).closest('.form-group').addClass('has-error');
            },
            unhighlight: function (element) {
                $(element).closest('.form-group').removeClass('has-error');
            }
        });
    });

我希望它使用required标签为每个元素自动验证表单.

I want that it automaticly validate the forms for every element with the required tag.

我该怎么做?

推荐答案

引用OP:

"我希望它为带有所需标记的每个元素自动验证表单."

引用OP评论:

我目前必须为每种形式调用$('#admin_settings_general').validate().如何在不限制为一种形式的情况下调用它?"

"i have to call $('#admin_settings_general').validate() for each of the forms currently. How can i call it without limiting it to one form?"


要在页面的 all 表单上正确初始化.validate(),请使用公共选择器,例如form标记本身(或class).由于无法将.validate()附加到表示多个form元素的任何 jQuery选择器,因此请使用jQuery .each().这就是该插件方法的设计方式.


To properly initialize .validate() on all forms on a page, use a common selector such as the form tag itself (or a class). Since you cannot attach .validate() to any jQuery selector that represents multiple form elements, use a jQuery .each(). This is simply how this plugins methods were designed.

$(document).ready(function() {

    $('form').each(function() {  // attach to all form elements on page
        $(this).validate({       // initialize plugin on each form
            // global options for plugin
        });
    });

});

工作演示: http://jsfiddle.net/6Fs9y/

Working DEMO: http://jsfiddle.net/6Fs9y/

这篇关于验证插件-自动验证页面上的多种形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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