简体   繁体   English

jquery validate:如何显示和隐藏错误摘要?

[英]jquery validate: How to show and hide error summary?

I have implemented an error summary according to this example here: 在这里根据这个例子实现了一个错误摘要

while I get it to display, I don't have a clue how to hide it once there are no errors remaining. 当我让它显示时,我没有线索如何在没有错误的情况下隐藏它。

I have fiddle here to demonstrate it: 我在这里用它来演示它:

type in anything in either of the two fields, while the error messages go away, the summary still remains. 在两个字段中的任何一个字段中输入任何内容,而错误消息消失,摘要仍然存在。 There must be an event that I need to subscribe to, but I can't figure it out. 必须有一个我需要订阅的事件,但我无法弄清楚。

$(document).ready(function () {

  var validator = validation_rules('#myform');
  validator.form();

  function validation_rules(form) {

    $.validator.addClassRules("fillone", {
      require_from_group: [1, ".fillone"]
    });

    var validator = $(form).validate({
      errorPlacement: function (error, element) {
        var field_error = $(form).find('#id_' + element.attr('name')).siblings('.field_error');
        if (field_error.length > 0) {
          error.appendTo(field_error);
        }

        $(field_error).show();
      },
      invalidHandler: function () {
        $("#validation_summary").text(validator.numberOfInvalids() + " field(s) are invalid");
      }

    });
    return validator;
  }

});

use the errorContainer option. 使用errorContainer选项。 This will show/hide the specified elements when the form becomes valid/invalid 当表单变为有效/无效时,这将显示/隐藏指定的元素

errorContainer:{"#validation_summary"} errorContainer:{ “#validation_summary”}

example is on http://jsfiddle.net/eDk2m/7 示例在http://jsfiddle.net/eDk2m/7上

edit after reading comment 阅读评论后编辑

there is an event to hook into - a good example is in the custom-methods-demo.html page of the plugin demos. 有一个事件要插入 - 一个很好的例子是插件演示的custom-methods-demo.html页面。 It looks like this 看起来像这样

var validator = $("form").bind("invalid-form.validate",  
function() { 
 var errorCount = validator.numberOfInvalids(); 
 // do other stuff here
}).validate({...});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM