如果使用jquery验证发生验证错误,则将类添加到父div [英] Add class to parent div if validation error occurs using jquery validation
问题描述
我的输入字段具有以下标记:
I have the following markup for my input fields:
<div class="required">
<label for="Name">Name <em>*</em></label>
<div class="input large">
@Html.TextBoxFor(m => m.Name)
@Html.ValidationMessageFor(m => m.Name)
</div>
</div>
如果发生错误,请使用jquery验证插件,然后它将在输入下方添加一个范围,并且还将类应用于输入本身,以便可以对错误进行样式设置.我还想做的是将类添加到父div中,其中包含必填类.因此,例如,如果发生错误,它将是<div class="required error">
,然后相对于应用于该字段的类固定了<div class="required valid">
,并消除了错误跨度.
Using the jquery validation plugin if an error occurs then it will add a span below the input and also apply a class to the input itself so that the error can be styled. What I also want to do is add a class to the parent div with a class of required. So for example if an error happens it will be <div class="required error">
and then when fixed <div class="required valid">
in relation to the classes that are applied to the field and removal of the error span.
我该怎么做?我看过文档站点,但是与将类添加到父div没有任何关系.
How would I do this? I've looked at the documentation site but haven't seen anything to do with adding a class to a parent div.
尽管如此,我还是要做类似的事情:
I've though about doing something like:
if ($('input').hasClass('valid')) {
$(this).parents('div.required').addClass('valid');
} else if ($('input').hasClass('input-validation-error')) {
$(this).parents('div.required').addClass('error');
}
但是如何通过验证运行它呢?就像发生错误或字段变为有效时一样.
but how can I run it sequence with the validation? Like when an error takes place or a field becomes valid.
推荐答案
我根本没有玩过,但是您也许可以使用 jQuery验证挂钩
I haven't played with this at all but you might be able to use jQuery Validate Hooks
这篇关于如果使用jquery验证发生验证错误,则将类添加到父div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!