【发布时间】:2014-08-28 20:25:51
【问题描述】:
我在我的 ASP.NET MVC 4 站点上遇到了 jQuery 验证问题。表格如下:
@model QuestionModel
@using ( Html.BeginForm() ) {
@Html.ValidationSummary( "Please correct the errors and try again." )
@Html.LabelFor( m => m.Author )
@Html.EditorFor( m => m.Author )
@Html.LabelFor( m => m.Question )
@Html.TextAreaFor( m => m.Question, new { @class = "tinymce" } )
@Html.LabelFor( m => m.Answer )
@Html.TextAreaFor( m => m.Answer, new { @class = "tinymce" } )
}
这是带有数据注释的模型:
public class QuestionModel
{
[Required]
public string Author { get; set; }
[Required]
public string Question { get; set; }
[Required]
public string Answer { get; set; }
}
在 Firebug 的控制台中,我可以分别验证表单的每个元素:
> $('#Author').valid()
true
> $('#Question').valid()
true
> $('#Answer').valid()
false
答案字段无效的原因是因为它是空白的:
> $('#Author').val()
"David Bowie"
> $('#Question').val()
"<p>What is my best song?</p>"
> $('#Answer').val()
""
然而,显然 jQuery validate 认为表单作为一个整体是有效的:
> $('form').valid()
true
这是为什么?
【问题讨论】:
-
这是您页面上唯一的表单吗?
-
奇怪。你能检查一下 textarea @Html.TextAreaFor( m => m.Answer, new { @class= "tinymce" } ) 生成的标记,看看生成的 ID 是否是 Answer?
-
@jrummell 是的。
-
@HaBo 给你,但它很丑:pastie.org/9510986
-
@HaBo 实际上我想通了。 TinyMCE 将 "display: none" 添加到 textarea 中,默认情况下验证函数会忽略:jqueryvalidation.org/validate/#ignore
标签: jquery forms asp.net-mvc-4 jquery-validate unobtrusive-validation