jQuery使用回调验证添加和删除类 [英] jQuery Validate add and remove classes using callbacks

查看:52
本文介绍了jQuery使用回调验证添加和删除类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery Validate插件,并且试图使用errorPlacementsuccess回调来添加和删除引导类has-error.

I am working with the jQuery Validate plugin and I am trying to use the errorPlacement and success callbacks to add and remove the bootstrap class has-error.

当前,errorPlacement回调有效,但由于success回调返回错误标签(我没有使用),所以我不确定该怎么做.我需要再次访问success中的输入,但是我不知道该怎么做.

Currently, the errorPlacement callback works but since the success callback returns the error label (which I'm not using) I'm not sure what to do. I need access to my input again in success but I don't know how to do it.

$('#msform').validate({
    submitHandler: function (form) {
        alert('valid form submitted');
        return false; // for demo
    },
    errorPlacement: function(error, element) {
        $(element).parent().addClass('has-error');
        $(element).css('border-color', '#a94442');
    },
    success: function(label) {
        label.removeClass('has-error').text('ok');
    },
    invalidHandler: function(event, validator) {
        var errors = validator.numberOfInvalids();
    }
});

有人可以帮忙吗?谢谢.

Can anybody help? Thanks.

我认为我应该通过推荐答案

所以我问了之后很快就找到了答案.我使用了错误的回调.对于这种类型的需求,您需要使用highlightunhighlight.但是由于某些原因,如果您不想添加标签,还需要定义errorPlacement.

So I found the answer pretty soon after I asked. I was using the wrong callbacks. For this type of desire you need to use highlight and unhighlight. But for some reason you also need to define errorPlacement if you don't want the added label.

$('#msform').validate({
    errorPlacement: function() {},
    highlight: function(element) {
        $(element).parent().addClass('has-error');
        $(element).css('border-color', '#a94442');
    },
    unhighlight: function(element) {
        $(element).parent().removeClass('has-error');
        $(element).css('border-color', '');
    },
    submitHandler: function (form) {
        alert('valid form submitted');
        return false; // for demo
    },
    invalidHandler: function(event, validator) {
        var errors = validator.numberOfInvalids();
    }
});

这篇关于jQuery使用回调验证添加和删除类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆