jquery validation对隐藏的元素不进行验证

validation默认不会对Hidden元素进行验证的,但最近使用了 thinkcmf开发了一个系统后台,在验证时发现隐藏的元素也进行了验证
刚开始以为是  validation版本问题(当前版本取消了默认不对 Hidden的验证 ),但查看了 validation源码却发现原来是 thinkcmf的作者对 validation进行了更改(取消了 默认不对 Hidden的验证 )
知道了原因更改进来就很简单了,在验证时手动再加上即可
//官网上的例子
$("#myform").validate({
  ignore: ":hidden",//不验证的元素
});

  

 
from  https://github.com/jzaefferer/jquery-validation/blob/master/src/core.js#LC603
//可以看到在源码中看到对:submit, :reset, :image, :disabled不进行验证和自定义的不验证规则
//我们只需要设置ignore 属性即可,当前也可以将元素设置为disabled
elements: function() {
			var validator = this,
				rulesCache = {};
			// Select all valid inputs inside the form (no submit or reset buttons)
			return $( this.currentForm )
			.find( "input, select, textarea, [contenteditable]" )
			.not( ":submit, :reset, :image, :disabled" )
			.not( this.settings.ignore )
			.filter( function() {
				var name = this.name || $( this ).attr( "name" ); // For contenteditable
				if ( !name && validator.settings.debug && window.console ) {
					console.error( "%o has no name assigned", this );
				}
				// Set form expando on contenteditable
				if ( this.hasAttribute( "contenteditable" ) ) {
					this.form = $( this ).closest( "form" )[ 0 ];
				}
				// Select only the first element for each name, and only those with rules specified
				if ( name in rulesCache || !validator.objectLength( $( this ).rules() ) ) {
					return false;
				}
				rulesCache[ name ] = true;
				return true;
			} );
		},

  

参考:
Git Hub: jquery-validation
jqueryvalidation documentation
jquery.validate插件验证隐藏input(有选项卡常用)



来自为知笔记(Wiz)



你可能感兴趣的:(jquery validation对隐藏的元素不进行验证)