我在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
为什么会这样?