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; } ); },
參考: