【问题标题】:jquery validate amount of errorsjquery 验证错误数量
【发布时间】:2015-09-23 21:31:28
【问题描述】:

我正在使用 jQuery Validate 1.11.1 和 jQuery 1.11.3。

我正在构建某种带有多个输入和表单元素的调查网站。

    $(function () {

    $.validator.setDefaults({
        errorElement: 'div',
        errorClass: 'alert alert-danger',
        errorPlacement: function(error, element) {
            error.insertAfter(element.closest('.panel'));
        }
    });

    $("#screenForm").validate({
        debug: true,
        focusInvalid: false,
        rules: {
                options: {
                    required: true
                },
                options2: {
                    required: true
                },
                checkbox1: {
                    required: true
                },
                checkbox1other: {
                    required: {       
                        depends: function(element) {
                            console.log($("#checkboxOther:checked").length == 1);
                            return $("#checkboxOther:checked").length == 1;
                        }
                    }
                },
                a_SQhyu5q__3__491: {
                    required: true
                },
                a_SQk8wb8__3__491: {
                    required: true
                }
        },
        groups: {
            matrix: "a_SQhyu5q__3__491 a_SQk8wb8__3__491"
        },
        messages: {
            checkbox1other: "Please fill in details"
        },
        success: function (element) {
            element.remove();
        },
        highlight: function(element) {
            $(element).closest('.panel').removeClass('<%=panelColor%>');
            $(element).closest('.panel').addClass('panel-danger');
        },
        unhighlight: function(element) {
            $(element).closest('.panel').removeClass('panel-danger');
            $(element).closest('.panel').addClass('<%=panelColor%>');
        }
    });
});

a_SQhyu5q__3__491a_SQk8wb8__3__491 显示在同一面板中。当两者都没有填写时,我会收到两条错误消息,显示这些字段是必需的。由于它们在同一个面板中,我想在发生这种情况时只显示一条错误消息,而不是两条。

我需要对“必需错误”的数量进行某种限制,所以这永远不会超过一个。

我尝试了组选项,但是当填写两个输入之一时,错误消息不会显示在第二个也应该填写的输入中。

对于选项,options2checkbox1 仍然有自己的消息,不应与其他消息组合。只是a_SQhyu5q__3__491a_SQk8wb8__3__491的错误信息应该合并。

【问题讨论】:

    标签: jquery jquery-validate


    【解决方案1】:

    刚刚发现组选项对我有用。 jQuery Validate 的一些外部问题或问题(不确定)使其看起来无法正常工作。当我使用 .valid() 函数手动检查时,错误消息立即出现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-06
      • 2016-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多