jQuery Validate 提交表单验证失败扩展方法
由于Validate沒有提供表單提交過后,驗證不通過觸發方法。這里做一下擴展。
引用場景:每次提交表單元素驗證不通過觸發方法
打開源代碼 找到focusInvalid 方法, 這里是提交表單時驗證不通過觸發方法,在這里做擴展是就好不 過的。
focusInvalid: function() {
if ( this.settings.focusInvalid ) {
try {
$(this.findLastActive() || this.errorList.length && this.errorList[0].element || [])
.filter(":visible")
.focus()
.trigger("focusin");
/* ?擴展方法failedCallback?*/
var failedCallback=this.settings.failedCallback; ?
? ? ? ? ? ?if($.type(failedCallback)==='function'){ ?
? ? ? ? ? ? ? ?failedCallback(this.errorList); ?
? ? ? ? ? ? ?}?
} catch(e) {
}
}
}
使用 ?
?$('#editForm').validate(
? ? ? { ??
? ? ? ? ? ?rules:..... ,
? ? ? ? ? ?failedCallback:function(errorList){
? ? ? // 循環驗證失敗元素 ??
for(var i=0; i<errorList.length; i++){
? ? ? ?var ?element=errorList[0].element;
? ? ? ?if($(element).attr("name") == 'birthday'){
? ? ? ? ? ........................................?
? ? ? ? ? ?}
? ? ? ??}
? ? ? ? ? ?}? ?
? ? ?}?
);
轉載于:https://blog.51cto.com/201510240609/1933670
總結
以上是生活随笔為你收集整理的jQuery Validate 提交表单验证失败扩展方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【BZOJ】3779 重组病毒
- 下一篇: Android Wear 唤醒热词会比“