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