如果容器被隐藏,则jQuery.validate无效

我将几个输入分成不同的容器(面板)。 我遇到的问题是,如果隐藏其中一个面板( style="display:none;" ),jQuery.validate插件不会validation这些输入。

我用一个小例子进行测试并发生同样的问题:

视图:

 @using (Html.BeginForm()) { @Html.ValidationSummary(true) 
Fields
@Html.LabelFor(model => model.UserName)
@Html.TextBoxFor(model => model.UserName) @Html.ValidationMessageFor(model => model.UserName)
@Html.LabelFor(model => model.FirstName)
@Html.TextBoxFor(model => model.FirstName) @Html.ValidationMessageFor(model => model.FirstName)
@Html.LabelFor(model => model.LastName)
@Html.TextBoxFor(model => model.LastName) @Html.ValidationMessageFor(model => model.LastName)
@Html.LabelFor(model => model.City)
@Html.TextBoxFor(model => model.City) @Html.ValidationMessageFor(model => model.City)

}

模型:

 public class UserModel { [Required] [StringLength(6, MinimumLength = 3)] [Display(Name = "User Name")] [RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed")] [ScaffoldColumn(false)] public string UserName { get; set; } [Required] [StringLength(8, MinimumLength = 3)] [Display(Name = "First Name")] public string FirstName { get; set; } [Required] [StringLength(9, MinimumLength = 2)] [Display(Name = "Last Name")] public string LastName { get; set; } [Required()] public string City { get; set; } } 

当div位于div中并且样式为"display:none;"时,客户端上未validation“UserName”属性"display:none;"

谢谢

它是不要validation隐藏输入字段的理想行为,使用jquery validate插件,您可以通过设置ignore选项来validation隐藏的输入字段

 $('#fromID').validate({ ignore: "", }); 

但是由于你在mvc3中使用不显眼的validation,使用jquery validate插件你不能自己初始化插件,你必须在初始化之后更改它的设置

 var validatorSettings = $.data($('form')[0], 'validator').settings; validatorSettings.ignore = ""; 

参考

这也是一个有用的博客post