jquery validation对隐藏的元素不进行验证
生活随笔
收集整理的這篇文章主要介紹了
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 contenteditableif ( !name && validator.settings.debug && window.console ) {console.error( "%o has no name assigned", this );}// Set form expando on contenteditableif ( this.hasAttribute( "contenteditable" ) ) {this.form = $( this ).closest( "form" )[ 0 ];}// Select only the first element for each name, and only those with rules specifiedif ( name in rulesCache || !validator.objectLength( $( this ).rules() ) ) {return false;}rulesCache[ name ] = true;return true;} );},
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 contenteditableif ( !name && validator.settings.debug && window.console ) {console.error( "%o has no name assigned", this );}// Set form expando on contenteditableif ( this.hasAttribute( "contenteditable" ) ) {this.form = $( this ).closest( "form" )[ 0 ];}// Select only the first element for each name, and only those with rules specifiedif ( name in rulesCache || !validator.objectLength( $( this ).rules() ) ) {return false;}rulesCache[ name ] = true;return true;} );},
參考: Git Hub:?jquery-validation jqueryvalidation documentation jquery.validate插件驗證隱藏input(有選項卡常用)
轉載于:https://www.cnblogs.com/huangtailang/p/5546367.html
總結
以上是生活随笔為你收集整理的jquery validation对隐藏的元素不进行验证的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: body中的font属性和color属性
- 下一篇: zznu 1632