jQueryvalidation插件:如何将错误视为警告并允许表单提交?

正如主题建议的那样,我使用的是bassistance.devalidation插件(http://docs.jquery.com/Plugins/Validation/),即使存在validation错误,我也希望能够提交表单。 基本上,我们只想使用插件来警告用户潜在的问题(请不要质疑可用性问题,我知道)。

那么,有没有办法轻松地做到这一点,还是应该侵入插件代码?

提前致谢!

我不会质疑这个问题的可用性问题:)。 这确实可以通过使用invalidHandler选项进行validation:

 $("#test-form").validate({ invalidHandler: function() { /* Allow the user to see the error messages before submitting: */ window.setTimeout(function() { $("#test-form")[0].submit(); }, 1000); } }); 

这是一个例子: http : //jsfiddle.net/ghDbd/

我遇到了类似的问题,我决定使用options.ignore属性。 http://docs.jquery.com/Plugins/Validation/validate#toptions

 $("form").validate({ rules: { "hard": {word_count:10}, // No more than 10 words "soft_and_hard": {word_count:[30,10]} // No more than 40 words }, ignore: ".error-okay" }); 

我使用自定义validation器来计算单词。 我们决定在X单词(他们要求的单词计数)后显示错误,如果他们有X + T单词则不允许表单提交。 当它介于X和X + T之间时,我会在元素中添加一个类“error-okay”(其中“。error-okay”是我作为ignore选项传递的)。

 jQuery.validator.addMethod("word_count", function(value, element, max) { var tolerance = 0; if (max instanceof Array){ tolerance = max[1]; max = max[0]; } var typedWords = jQuery.trim(value).split(' ').length; if(typedWords <= max + tolerance) $(element).addClass("error-okay"); else $(element).removeClass("error-okay"); return (typedWords <= max); }, function(max, ele){ var tolerance = ""; if (max instanceof Array){ tolerance = "
Definitly no more than " + ( max[0] + max[1] ) + " words."; max = max[0]; } return "Please enter " + max +" words or fewer. You've entered " + jQuery.trim($(ele).val()).split(' ').length + " words." + tolerance; });

如果你在一个字段上有多个validation,你可能最好写一个invalidHandler函数,可能将它与我使用的添加类组合(例如:“word-count-okay”)。