[英]How do I prevent jquery validation from highlighting my fields when I unhide them?
On my forms I have numerous optional fields that are only needed if a prior checkbox or radio button value is supplied. 在我的表单上,我有许多可选字段,只有在提供了先前的复选框或单选按钮值的情况下才需要。 To keep the form clean I'm hiding those additional fields until they're needed.
为了保持表格清洁,我将隐藏这些其他字段,直到需要它们为止。
The issue I'm having is when those fields are unhidden they're being highlighted as if they had an error, most likely because they're marked as conditionally required. 我遇到的问题是,当取消隐藏这些字段时,它们会像出现错误一样被突出显示,很可能是因为将它们标记为有条件的。 Is there any way to prevent this from happening so the validation only happens on submit?
有什么办法可以防止这种情况的发生,因此验证仅在提交时进行?
I'm using ExpressiveAnnotations and unobtrusive validation is wiring everything up. 我正在使用ExpressiveAnnotations,并且不干扰用户的验证将所有内容都连接了起来。 My jquery validation config is:
我的jquery验证配置是:
$.validator.setDefaults({ highlight: (element) => { $(element).closest('.form-group').addClass('has-error'); }, unhighlight: element => { $(element).closest('.form-group').removeClass('has-error'); } });
To hide or show the extra fields I've wrapped them in a div
and toggle the visibility with a class called hidden
. 为了隐藏或显示额外的字段,我将它们包装在
div
并使用名为hidden
的类切换可见性。
I'm sure you've tried using 我确定您已尝试使用
display:block;
instead of 代替
visibility:visible;
in your CSS. 在您的CSS中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.