我來到了這個職位,我遇到了同樣的問題後填寫,然後清除所需的表單字段。我用一個例子合併Twitter的引導形式標記了這裏找到jquery.validate.js:http://alittlecode.com/jquery-form-validation-with-styles-from-twitter-bootstrap/
我解決它使用下面的代碼:
onfocusout: function(label) {
if($(label).val() == ''){
$(label).closest('.control-group').removeClass('success');
}
}
這裏的上下文中的完整的腳本:
$(document).ready(function() {
$('#contact_form').validate({
rules: {
name: {
minlength: 2,
required: true
},
email: {
required: true,
email: true
},
message: {
minlength: 2,
required: true
}
},
onfocusout: function(label) {
if($(label).val() == ''){
$(label).closest('.control-group').removeClass('success');
}
},
highlight: function(label) {
$(label).closest('.control-group').addClass('error');
},
success: function(label) {
label
.text('OK!').addClass('valid')
.closest('.control-group').addClass('success');
},
submitHandler: function(form) {
$(form).ajaxSubmit({
target: '#success',
success: function() {
$('#contact_form_wrapper').hide();
$('#success').html('<h3>Thank you for your enquiry</h3><p class="intro-text">We will endeavour to respond to your message within 72 hours.</p>').fadeIn('slow');
}
});
}
});
});
嗯,我已經在輸入標籤中定義了所需的類,但是如果該字段爲空,錯誤仍然不會顯示。還有其他想法嗎? – Cofey 2011-04-09 06:47:39
有關錯誤未顯示onblur的示例,請參見插件演示頁面,如果在輸入字段具有「錯誤」類時該字段爲空:http://jquery.bassistance.de/validate/demo/ – Cofey 2011-04-09 15:37:02