在我的 ASP.NET MVC4 应用程序中,我试图添加图像而不是验证消息(图像具有包含错误消息的工具提示)。
我唯一的问题是在出现验证错误消息时能够显示图像(类:field-validation-error)。
如何在图片内部html包含类field-validation-error时才显示图片?
或者是否有可能以某种方式拦截当 ValidationMessageFor 将错误从有效更改为错误时发生的事件,反之亦然?
最佳答案
How can I display the image only when the inner html of the image contains the class field-validation-error?
事实上,没有办法动态检测类的变化。但是,有一些解决方法。
我认为实现此目的的最佳方法是在 jquery 中使用 change
事件。如您所知,验证仅适用于表单输入。因此,如果您使用非侵入式验证,则可以将以下事件添加到所有输入元素并孵化其值的更改:
$("input").change(function () {
if ($(this).hasClass("field-validation-error")) {
// code to show the relevant image...
}
});
关于c# - 检测何时出现验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18431924/