简体   繁体   English

jquery validate plugin的问题

[英]issues with jquery validate plugin

I am using a jquery validation Plugin, I had the following code for validation which is troubling, it is just a specification how i want the error should display on the screen 我正在使用一个jquery验证插件,我有以下代码进行验证这是令人不安的,它只是一个规范我想要的错误应该显示在屏幕上

jQuery(document).ready(function() {
    jQuery('form.CWvalidate').each(function() {
        var alertID = jQuery(this).attr('id');
        jQuery(this).prepend('<div class="CWalertBox alertText validationAlert" style="display:none;"></div><div class="CWclear"></div>');
    });
    jQuery('form.CWvalidate select').change(function() {
        jQuery(this).blur();
    });
    jQuery.validator.setDefaults({
        focusInvalid: true,
        onkeyup: false,
        onblur: true,
        errorClass: "warning",
        errorElement: "span",
        errorPlacement: function(error, element) {
            jQuery(element).siblings('label').addClass('warning');
        },
        showErrors: function(errorMap, errorList) {
            if (this.numberOfInvalids() > 0) {
                var formID = jQuery(this.currentForm).attr('id');
                if (formID == 'CWformCustomer') {
                    var validationMessage = 'Complete all required information';
                } else if (formID == 'CWformLogin') {
                    var validationMessage = '';
                } else if (formID == 'CWformOrderSubmit') {
                    var validationMessage = 'Complete all required information';
                } else if (formID == 'CWformAddToCartWindow') {
                    var validationMessage = 'Select at least one item';
                } else if (formID == 'CWformAddToCart') {
                    var validationMessage = 'Choose from below options before adding to cart';
                } else {
                    var validationMessage = 'Complete your selection';
                };
                if (!(validationMessage == '')) {
                    jQuery(this.currentForm).find('.errormsg').show().html(validationMessage);
                };
                this.defaultShowErrors();
            } else {
                jQuery(this.currentForm).children('div.validationAlert').empty().hide();
                jQuery(this.currentForm).children('span.warning').remove();
                jQuery(this.currentForm).children('.warning').removeClass('warning');
            }
        }
    });
    jQuery('form.CWvalidate').each(function() {
        jQuery(this).validate();
    });
});

but whenever I ma using this classwhich is CWvalidate , the form validates but the submithandler of the form does not which i use it like below code: 但每当我使用这个CWvalidate CWvalidate ,表单都会验证,但表单的submithandler不会像我在下面的代码中那样使用它:

$("#dataModification").validate({
    submitHandler: function(form) {
    var datastring = $(form).serialize();
    $.ajax({
        type:"post",
        url: 'xyz.cfm',
        data: datastring,
        success: function(data) {
            var obj = $.trim(data);

            if (obj == 'success'){
                parent.$.fancybox.close();
            }else{
                alert(obj);
            }
          }
       });
    }
});

What is wrong I am stuck and do not know what is going on... 有什么问题我被困住了,不知道发生了什么......

You seem to be calling .validate() twice on the same form... 你似乎在同一个表单上两次调用.validate() ...

Here: 这里:

jQuery(document).ready(function() {
    ....
    jQuery('form.CWvalidate').each(function() {
        jQuery(this).validate();
    });
});

And here: 和这里:

$("#dataModification").validate({
    submitHandler: function(form) { 
    ...

Only the first instance is used and any subsequent instances are ignored, which explains why your submitHandler is not working and the form simply refreshes. 仅使用第一个实例,并忽略任何后续实例,这解释了为什么您的submitHandler无法正常工作且表单只是刷新。 It also explains why it only fails when using your CWvalidate class. 它还解释了为什么它只在使用CWvalidate类时失败。

To fix it, you'll have to figure out how to call .validate() one time per each form. 为了解决这个问题,你就必须弄清楚如何调用.validate()每每个窗体一次

One possible workaround would be to put your ajax URL within the action attribute of the form tag and then retrieve this URL within .ajax() using .attr('action') . 一种可能的解决方法是将ajax URL放在form标签的action属性中,然后使用.attr('action').ajax()检索此URL。 Then your submitHandler can be generic enough to be included within setDefaults() . 然后你的submitHandler可以足够通用,可以包含在setDefaults()

submitHandler: function(form) {
    var datastring = $(form).serialize();
    $.ajax({
        type: "post",
        url: $(form).attr('action'), ...

Also note: There is no such option called onblur for this plugin. 另请注意:此插件没有名为onblur选项。 It's onfocusout and you cannot set it to true without breaking the plugin. 它是onfocusout ,你不能在不破坏插件的情况下将其设置为true It can only be set to false or a function . 它只能设置为false或函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM