Customising event delegates in the jQuery validation plug-in

馋奶兔 提交于 2019-12-23 09:16:51

问题


I am currently setting up the jQuery validation plug-in for use in our project.

By default, there are some events automatically set up for handling. I.e. focus in/out, key up events on all inputs fire validation. I want it to only fire when the submit button is clicked.

This functionality seems to be in-built into the plug-in, which is making it difficult to do this (without modifying the plug-in code, Not What I Want To Do).

I have found the eventDelegate function calls in the plugin code prototype method:

        $(this.currentForm)
            .validateDelegate(":text, :password, :file, select, textarea", "focusin focusout keyup", delegate)
            .validateDelegate(":radio, :checkbox, select, option", "click", delegate);

When I remove these lines from the plug-in I get my result, however I would much rather do something Outside the plug-in to achieve this.

Can anybody please help me? If you need any more details, please let me know. I have searched google with little success.

Thanks

EDIT: I am basically trying to validate the form Only when the submit event is fired. By default, the plug-in validates every time focus is lost in an input control.


回答1:


Found the answer. It was (hidden?) away as part of the options in the validate method.

See the options onfocusout etc on this page: http://docs.jquery.com/Plugins/Validation/validate#options

which I can set to false.

Here is my code which sets up my validator (hopefully others will find this useful):

$(document).ready(function() {
    $("form").each(function() {
        $(this).validate({
            validateDelegate: function() { },
            onsubmit: true,
            onkeydown: false,
            onkeyup: false,
            onfocusin: false,
            onfocusout: false,

            errorContainer: "#PanelError",
            errorLabelContainer: "#PanelError ul",
            wrapper: "li",
            ignoreTitle: true,
            errorClass: "Error",

            highlight: function(element, errorClass, validClass) {
                $(element).addClass(errorClass).removeClass(validClass);
                $(element.form).find("#" + element.id)
                        .addClass(errorClass);
            },
            unhighlight: function(element, errorClass, validClass) {
                $(element).removeClass(errorClass).addClass(validClass);
                $(element.form).find("#" + element.id)
                        .removeClass(errorClass);
            }
        });
    });
});


来源:https://stackoverflow.com/questions/2762458/customising-event-delegates-in-the-jquery-validation-plug-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!