2016-08-12 59 views
1

我正在使用jQuery Validate插件,我試圖使用errorPlacementsuccess回調來添加和刪除引導類has-errorjQuery驗證使用回調添加和刪除類

目前,errorPlacement回調工作,但自success回調返回錯誤標籤(我不使用),我不知道該怎麼做。我需要再次訪問我的輸入success,但我不知道該怎麼做。

$('#msform').validate({ 
    submitHandler: function (form) { 
     alert('valid form submitted'); 
     return false; // for demo 
    }, 
    errorPlacement: function(error, element) { 
     $(element).parent().addClass('has-error'); 
     $(element).css('border-color', '#a94442'); 
    }, 
    success: function(label) { 
     label.removeClass('has-error').text('ok'); 
    }, 
    invalidHandler: function(event, validator) { 
     var errors = validator.numberOfInvalids(); 
    } 
}); 

任何人都可以幫忙嗎?謝謝。

編輯:

我想我應該通過combining custom error placement with success is not working jquery validate plugin使用高亮和unhighlight。

回答

1

所以我問起後很快就找到了答案。我使用了錯誤的回調。對於這種類型的願望,你需要使用highlightunhighlight。但出於某種原因,如果您不想添加標籤,您還需要定義errorPlacement

$('#msform').validate({ 
    errorPlacement: function() {}, 
    highlight: function(element) { 
     $(element).parent().addClass('has-error'); 
     $(element).css('border-color', '#a94442'); 
    }, 
    unhighlight: function(element) { 
     $(element).parent().removeClass('has-error'); 
     $(element).css('border-color', ''); 
    }, 
    submitHandler: function (form) { 
     alert('valid form submitted'); 
     return false; // for demo 
    }, 
    invalidHandler: function(event, validator) { 
     var errors = validator.numberOfInvalids(); 
    } 
});