I have two jQuery.validator.addMethod, named "A" and "B". Is it possible to set conditional rules on the fields based on different methods? For example: $("#form2").validate( { rules: { inputVal: { //if condition = true, A:true, else run B:true } } ...}); 解决方案 No, you cannot set different rules based on different conditions within the .validate() method. The object literal for each field can only contain a comma separated list of key:value pairs representing the desired methods and parameters.However, you are allowed to conditionally set the parameter for each method using the depends property, effectively achieving the same thing by toggling the rule on or off...$("#form2").validate( { rules: { inputVal: { // <- field name method_A: depends: function(element) { // activate/disable method A return (condition) ? true : false; } }, method_B: depends: function(element) { // disable/activate method B return (condition) ? false : true; } } } }, ...});DEMO: http://jsfiddle.net/nL2sk069/Otherwise, you could simply write a new method that combines the functionality of methods A & B, and just evaluate your condition there.var message;$.validator.addMethod('method_C', function(value, element, param) { var result; if (condition) { // validate value as per A result = (value === "A") ? true : false; // (pass or fail) message = "custom message A"; } else { // validate value as per B result = (value === "B") ? true : false; // (pass or fail) message = "custom message B"; } return this.optional(element) || result;}, function() { return message; });DEMO: http://jsfiddle.net/3uheeetx/ 这篇关于基于jQuery.validator.addMethod的字段上的条件规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-19 05:11