javascript 如何检测 jQuery 验证何时完成,并根据该事件调用某些内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6422327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:36:53  来源:igfitidea点击:

How can I detect when jQuery Validation is done, and call something based on that event?

javascriptjqueryjquery-validatecufon

提问by Iladarsda

I'm new to jQuery.

我是 jQuery 的新手。

Working with jQuery validation plugin & cufon at the same time is giving me really hard time.

同时使用 jQuery 验证插件和 cufon 给我带来了非常困难的时间。

Basically, I want to detect event once jQuery Validation did what it had to do and call Cufon.refresh() straight after it.

基本上,我想在 jQuery 验证完成它必须做的事情后检测事件,并在它之后直接调用 Cufon.refresh()。

$('#commentForm').validate({
    rules: {
        password: {
            required: true,
            minlength: 8,
            maxlength: 8,
            number: true
        },
    }
});

We are expecting <label class="error"> SOME TEXT </label>when form is not valid.

我们期待<label class="error"> SOME TEXT </label>表单无效时。

And once that created I want to Cufon.refresh() on that label created by jQuery Validation. How can I detect when jQuery Validation is done, and call something based on that event?

一旦创建,我想在由 jQuery 验证创建的标签上使用 Cufon.refresh()。 如何检测 jQuery 验证何时完成,并根据该事件调用某些内容?

Any help much appreciated. Regards, Piotr

非常感谢任何帮助。问候, 彼得

回答by Iladarsda

Thanks to @Ariel - if there is a 'success' there has to be a 'not-success' as well, so..

感谢@Ariel - 如果有一个“成功”,那么也必须有一个“不成功”,所以..

Working code:

工作代码:

$('#commentForm').validate({
    rules: {
        password: {
            required: true,
            minlength: 8,
            maxlength: 8,
            number: true
        }
    },
    showErrors: function(errorMap, errorList) {
        this.defaultShowErrors();
        Cufon.refresh();
        //alert('not valid!')
    },
    success: function() {
        //alert('valid!')
    }
});

Thanks again for the idea!

再次感谢您的想法!

回答by Ariel

Use the successoption:

使用success选项:

$('#commentForm').validate({
    rules: {
        password: {
            required: true,
            minlength: 8,
            maxlength: 8,
            number: true
        },
    }
    success: function() { .... }
});

Note that you have an extra comma after the close brace for the password object. This will give an error in IE.

请注意,在密码对象的右括号后有一个额外的逗号。这将在 IE 中给出错误。

回答by k3malb3y

 <script src="js/validate/jquery-1.11.1.min.js"></script>
 <script src="js/validate/jquery.validate.min.js"></script>
 <script src="js/validate/additional-methods.min.js"></script>

<script>
    jQuery.validator.setDefaults({
        success:  "valid"
    });

    var form = $("#myform");
    form.validate({
        rules: {
           name: {required: true, minlength: 2},
            lastname: {required: true, minlength: 2}
        }

    });

    $("#button").click(function() {
        if(form.valid() == true ) { // here you check if validation returned true or false 
            $("body").addClass("loading");
        }
    })

</script>

回答by José Valente

submitHandler: { function(){ bla bla }}

This will allow you to execute code upon the completion of the validate. you will need to place a submit form snippet though, since it replaces the default handler.

这将允许您在验证完成后执行代码。不过,您需要放置一个提交表单片段,因为它替换了默认处理程序。

EDIT:

编辑:

    // specifying a submitHandler prevents the default submit 
    submitHandler: function() { 
        alert("submitted!"); 
    }, 
    // set this class to error-labels to indicate valid fields 
    success: function(label) { 
        // set   as text for IE 
        label.html(" ").addClass("checked"); 
    } 

You can use either to do what you want. submitHandler allows you to stop the submit and execute code instead ( you can possibly use it to perform code BEFORE you submit it ) or success to execute code after the submit.

您可以使用其中任何一个来做您想做的事。submitHandler 允许您停止提交并执行代码(您可以在提交之前使用它来执行代码)或在提交后成功执行代码。

回答by Shea Scott

Put it inside the errorPlacementoption.

把它放在errorPlacement选项里面。

errorPlacement: function(error, element) {
  error.appendTo( element.parent() );
  yourCodeHere();
}

回答by RaviRokkam

$(document).ready(function() {  
    $('#commentForm').submit(function(){
        var validationResponse = $('#commentForm').valid();

        if(validationResponse) {
            // when true, your logic
        } else {
            // when false, your logic
            return false;
        }
    });

    $("#commentForm" ).validate({
        rules: {
            "first_name": {
                required: true
            }
        },
        messages: {
            "first_name": {
                required: "First Name can not be empty"
            }
        }
    });
});