2012-04-17 36 views
0

我有一个与我的代码,我有一个特定的textarea,我使用主.error类和另一个.textAreaError,放置错误框是我想要的但我不确定我将配置textarea错误加载在这个特定的框中。jQuery设置两个类的验证错误框

From a previous question我被给了以下jQuery行function(error, element) { error.appendTo(element.siblings("label")); },但我不确定如何将它应用到下面的代码中。

的jQuery:

jQuery(document).ready(function() { 
    //Home Validation 
    onfocusout: true, 
    $("#quote").validate({ 
     rules:{ 
      companyName:{ 
       required: true, 
       url: true 
      } 
     } 
    }); 

HTML:

<div class="error textAreaError">Error Here</div>

+0

这是无效的代码。你不能只拥有“对话:真实,”独自坐着。它是插件的一个可选参数,因此它需要与其他可选参数一起使用。另外,请仔细查看[代码**和注释**中对您的其他问题的接受答案](http://stackoverflow.com/a/10151646/594235)以获取完整答案。提示:'errorPlacement:' – Sparky 2012-04-17 23:05:32

回答

1

从看documentation的验证插件,它看起来像你可以控制误差与位置使用函数(error,element)的errorPlacement参数。代码看起来是这样的:

jQuery(document).ready(function() { 
    //Home Validation 
    $("#quote").validate({ 
     onfocusout: true, 
     rules:{ 
      companyName:{ 
       required: true, 
       url: true 
      } 
     }, 
     errorPlacement: function(error, element) { 
      if (element.is("textarea")) 
       error.appendTo(".textAreaError"); 
      else 
       error.appendTo(".generalError"); 
     } 
    }); 
}); 

你必须给一般错误框generalError类。或者,给出错误框的ID并引用那些(例如“#textAreaError”和“#generalError”)。

+0

我看到它的方式,主要问题是在哪里应用该功能。但我正在研究代码示例... – Trax72 2012-04-17 23:03:36

+0

OP需要看到它,因为他完全忽略了[本答案中的评论]中包含的代码的关键部分(http://stackoverflow.com/a/10151646/ 594235)。 – Sparky 2012-04-17 23:07:08

+0

我怎么能做多一个appendTo?我有.error作为通用错误框和textarea的专用错误框 – 2012-04-17 23:08:17