带有jQuery验证插件的Bootstrap [英] Bootstrap with jQuery Validation Plugin
本文介绍了带有jQuery验证插件的Bootstrap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用jQuery Validation Plugin将验证添加到表单中,但是在使用输入组时,该插件会在输入错误消息时遇到问题.
I am trying to add validation to my form with jQuery Validation Plugin, but I'm having a problem where the plugin puts the error messages when I'm using input groups.
$('form').validate({
rules: {
firstname: {
minlength: 3,
maxlength: 15,
required: true
},
lastname: {
minlength: 3,
maxlength: 15,
required: true
}
},
highlight: function(element) {
$(element).closest('.form-group').addClass('has-error');
},
unhighlight: function(element) {
$(element).closest('.form-group').removeClass('has-error');
}
});
我的代码: http://jsfiddle.net/hTPY7/4/
推荐答案
为与twitter bootstrap 3完全兼容,我需要重写一些插件方法:
for total compatibility with twitter bootstrap 3, I need to override some plugins methods:
// override jquery validate plugin defaults
$.validator.setDefaults({
highlight: function(element) {
$(element).closest('.form-group').addClass('has-error');
},
unhighlight: function(element) {
$(element).closest('.form-group').removeClass('has-error');
},
errorElement: 'span',
errorClass: 'help-block',
errorPlacement: function(error, element) {
if(element.parent('.input-group').length) {
error.insertAfter(element.parent());
} else {
error.insertAfter(element);
}
}
});
请参阅示例: http://jsfiddle.net/mapb_1990/hTPY7/7/
这篇关于带有jQuery验证插件的Bootstrap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文