简体   繁体   English

需要Jquery Validation Plugin方法(dependency-expression)使用ok但回调不起作用

[英]Jquery Validation Plugin Method required( dependency-expression ) use ok but callback not working

i'm trying to make this work; 我正努力做这项工作; this validate is applied to a payment form, and as you can see, there are only 3 requierd inputs, if and only if 此验证适用于付款表单,如您所见,只有3个需求输入,当且仅当

input[type=radio]:checked').val() != "CB";

wich means if user is about to pay with something else than a credit card. 这意味着用户是否要用信用卡支付其他费用。 Following is the complete code to validate my form. 以下是验证我的表单的完整代码。

$("#paiement").validate({
        errorClass: "invalid",
        validClass: "success",
        rules: {
            referenceTypeComplementBancaire: true,
            banque: {
                required: function(nomBanque){
                    return $('#paiement input[type=radio]:checked').val() != "CB";
                }
            },
            numeroComplementBancaire: {
                required: function(numeroCompl){
                    return $('#paiement input[type=radio]:checked').val() != "CB";
                }  
            },
            montantComplementBancaire: {
                required: function(montantCompl){
                    var logg = montantRentre >= panierTotal;
                    console.log(logg);
                    return montantRentre >= panierTotal;
                }
            }
        },
        messages: {
         referenceTypeComplementBancaire: "",
         banque:"",
         numeroComplementBancaire:"",
         montantComplementBancaire:""
        }
    });
}

Nothing hard, really. 没什么难的,真的。 But, whats hard for me to understand is why montantComplementBancaire isn't validated, even if my console logging shows me 'true' or 'false' in the right time. 但是,我很难理解为什么montantComplementBancaire没有经过验证,即使我的控制台记录在合适的时间显示我'真'或'假'。 What am I doing wrong ? 我究竟做错了什么 ?

----------------------------EDIT------------------------------------------ - - - - - - - - - - - - - - 编辑 - - - - - - - - - - - ---------------------

I think there's been some kind of misunderstanding, my fault. 我认为存在某种误解,我的错。 Sorry folks. 对不起,伙计们。 Here's how the form finally looks like: 这是表单最终的样子:

        $("#paiement").validate({
        errorClass: "invalid",
        validClass: "success",
        rules: {
            referenceTypeComplementBancaire: true,
            banque: {
                required: function(nomBanque){
                    return $('#paiement input[type=radio]:checked').val() != "CB";
                }
            },
            numeroComplementBancaire: {
                required: function(numeroCompl){
                    return $('#paiement input[type=radio]:checked').val() != "CB";
                }  
            },
            montantComplementBancaire: {
                required: function(){
                    var logg = panierTotal > montantRentre; 
                    console.log(logg); 
                    return panierTotal > montantRentre; 
                }
            }
        },
        messages: {
         referenceTypeComplementBancaire: "",
         banque:"",
         numeroComplementBancaire:"",
         montantComplementBancaire:""
        }
    });

Where did I go wrong ? 我哪里做错了 ? Not only do I want "montantComplementBancaire" to be validated, but I want it to be valid IF and ONLY IF its >= to panierTotal 我不仅要对“montantComplementBancaire”进行验证,而且我希望它是有效的IF,并且只要它> = to panierTotal

Right now, I'm only testing wether or not to validate it, but what I'm looking for is a way to return 'valid' when montantRentre >= to panierTotal . 现在,我只测试是否验证它,但我正在寻找的是当montantRentre> = panierTotal时返回'valid'的方法。

Am I making tjis any clearer ? 我让tjis更清楚了吗?

try return logg; 尝试return logg; or return (montantRentre >= panierTotal); 或者return (montantRentre >= panierTotal);

Your code looks ok and seems to match the example here ( http://docs.jquery.com/Plugins/Validation/Methods/required#dependency-callback ). 您的代码看起来没问题,似乎与此处的示例匹配( http://docs.jquery.com/Plugins/Validation/Methods/required#dependency-callback )。 The required property doesn't need to be an object with a depends property, it can be a dependency-callback function like you have done. required属性不需要是具有depends属性的对象,它可以像您所做的那样是依赖回调函数。

You could also try 你也可以试试

if(montantRentre >= panierTotal) {
  return true;
} else {
  return false;
}

I don't think your validation is setup properly. 我认为您的验证设置不正确。 The "required" property of each form element should be a JavaScript Object with the "depends" property set to a function, presuming what you're trying to do is conditional requirements. 每个表单元素的“required”属性应该是一个JavaScript对象,其中“depends”属性设置为一个函数,假设你要做的是条件要求。 See the "rules" entry on the validation options page: http://docs.jquery.com/Plugins/Validation/validate#toptions 请参阅验证选项页面上的“规则”条目: http//docs.jquery.com/Plugins/Validation/validate#toptions

I think the fixed code would be: 我认为固定代码是:

$("#paiement").validate({
    errorClass: "invalid",
    validClass: "success",
    rules: {
            referenceTypeComplementBancaire: 'required',
            banque: {
                    required: {
                        depends: function(nomBanque){
                            return $('#paiement input[type=radio]:checked').val() != "CB";
                        }
                    }
            },
            numeroComplementBancaire: {
                    required: {
                        depends: function(numeroCompl){
                            return $('#paiement input[type=radio]:checked').val() != "CB";
                        }  
                    }
            },
            montantComplementBancaire: {
                    required: {
                        depends: function(montantCompl){
                            var logg = montantRentre >= panierTotal;
                            console.log(logg);
                            return montantRentre >= panierTotal;
                        }
                    }
            }
    },
    messages: {
     referenceTypeComplementBancaire: "",
     banque:"",
     numeroComplementBancaire:"",
     montantComplementBancaire:""
    }
    });
}
required: function(montantCompl){ 
    var logg = montantRentre >= panierTotal; 
    console.log(logg); 
    return montantRentre >= panierTotal; 
}

why do you define an argument function(montantCompl){ that you never use? 为什么要定义一个你从不使用的参数function(montantCompl){ Did you mean to actually make your comparison against that? 你的意思是真的要与之进行比较吗? Even if you didn't, it is not really a good idea to use global variables for comparison inside anonymous functions as it leads to ambiguity, like right now when I'm trying to help you out with your issue. 即使你没有,在匿名函数中使用全局变量进行比较并不是一个好主意,因为它会导致模糊,就像现在我正试图帮助你解决问题一样。 If these values are defined somewhere, please includ them in your question including any points where they change state. 如果在某处定义了这些值,请在您的问题中包括它们,包括它们改变状态的任何点。

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

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