2010-09-11 119 views
0

我想添加一个淡入/淡出效果的jquery的验证显示错误消息。有什么办法做到这一点?我可以使用一个div并在他们身上分开工作吗?该插件是否包含这种效果?jquery验证:添加淡入/淡出效果到错误消息

我使用这个代码放置错误信息(我需要它的正确放置):

$("#commentForm2").validate({ 


     errorElement: "div", 

     errorPlacement: function(error, element) { 
      offset = element.offset(); 
      error.insertBefore(element) 
      error.addClass('message'); // add a class to the wrapper 
      error.css('position', 'absolute'); 
      error.css('left', offset.left + element.outerWidth()); 
      error.css('top', offset.top); 
     } 

    }); 
+0

如果你显示一些代码,我可以给你一个例子...和什么插件?你不需要一个淡入淡出 – 2010-09-11 19:43:44

+0

@Capt - 他正在谈论jQuery验证插件:http://bassistance.de/jquery-plugins/jquery-plugin-validation/并使* it *与淡入淡出。 – 2010-09-11 19:53:03

+0

我添加了一些代码。 – andandandand 2010-09-11 19:53:37

回答

1

我意识到这是一个超级老问题,但我找不到任何地方这个答案。这是我最终使用的解决方案:http://jsfiddle.net/MkARD/

想法是覆盖处理显示和隐藏错误的函数,以使用SlideDown和SlideOut而不是Show和Hide。这也可以应用于FadeIn和FadeOut。覆盖这些功能似乎已经在代码中得到支持,但没有记录。

此外,我选择在输入焦点时清除错误。如果您希望在其他事件中清除错误,则必须找到依赖于哪个功能并在其中进行更改。

希望这可以帮助别人!这些是我重写的功能:

onfocusin: function(element, event) { 
     this.lastActive = element; 

     // hide error label and remove error class on focus if enabled 
     if (this.settings.focusCleanup && !this.blockFocusCleanup) { 
      if (this.settings.unhighlight) { 
       this.settings.unhighlight.call(this, element, this.settings.errorClass, this.settings.validClass); 
      } 
      this.addWrapper(this.errorsFor(element)).slideUp(); 
     } 
    }, 
    showErrors: function() { 
     var i, elements; 
     for (i = 0; this.errorList[i]; i++) { 
      var error = this.errorList[i]; 
      if (this.settings.highlight) { 
       this.settings.highlight.call(this, error.element, this.settings.errorClass, this.settings.validClass); 
      } 
      this.showLabel(error.element, error.message); 
     } 
     if (this.errorList.length) { 
      this.toShow = this.toShow.add(this.containers); 
     } 
     if (this.settings.success) { 
      for (i = 0; this.successList[i]; i++) { 
       this.showLabel(this.successList[i]); 
      } 
     } 
     if (this.settings.unhighlight) { 
      for (i = 0, elements = this.validElements(); elements[i]; i++) { 
       this.settings.unhighlight.call(this, elements[i], this.settings.errorClass, this.settings.validClass); 
      } 
     } 
     this.toHide = this.toHide.not(this.toShow); 
     this.hideErrors(); 
     this.addWrapper(this.toShow).slideDown(); 
    }