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

发布于 2024-11-16 18:48:10 字数 559 浏览 4 评论 0原文

我是 jQuery 新手。

使用 jQuery 验证插件 &同时,cufon 也给我带来了很大的困难。

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

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

我们期待

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

非常感谢任何帮助。 问候, 皮奥特尔

I'm new to jQuery.

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

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

$('#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.

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?

Any help much appreciated.
Regards,
Piotr

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

望笑 2024-11-23 18:48:10

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

工作代码:

$('#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 to @Ariel - if there is a 'success' there has to be a 'not-success' as well, so..

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!

千仐 2024-11-23 18:48:10

使用 success 选项:

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

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

Use the success option:

$('#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.

逆光下的微笑 2024-11-23 18:48:10
 <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>
 <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>
§对你不离不弃 2024-11-23 18:48:10
submitHandler: { function(){ bla bla }}

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

编辑:

    // 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"); 
    } 

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

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.

情愿 2024-11-23 18:48:10

将其放在 errorPlacement 选项中。

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

Put it inside the errorPlacement option.

errorPlacement: function(error, element) {
  error.appendTo( element.parent() );
  yourCodeHere();
}
半﹌身腐败 2024-11-23 18:48:10
$(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"
            }
        }
    });
});
$(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"
            }
        }
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文