使用jquery验证器。使用Tab键导航表单时不执行验证。
$(function () {
var validator = $("#myForm").validate({
onfocusout: function(element) {
this.element(element);
},
rules: {
fname: "required",
check: "required",
color: "required"
},
messages: {
fname: "Enter your firstname",
check: "you know you do",
color: "pick one!",
},
});
});
我尝试在复选框上执行on blur。但是,事件是在表单加载时触发的。 Here's the improved fiddle
$('#check').on('blur', function() {
$("#myForm").validate().element( this );
}).blur();
答案 0 :(得分:4)
关于onfocusout问题的解决方案/工作是对复选框和无线电使用on blur。
$(function () {
var validator = $("#myForm").validate({
onfocusout: function(element) {
this.element(element);
},
rules: {
fname: "required",
check: "required",
radio: "required",
color: "required"
},
messages: {
fname: "Enter your firstname",
check: "check your checkbox",
radio: "check your radio",
color: "pick one!",
},
});
$('#check').on('blur', function() {
$("#myForm").validate().element( this );
});
$('#radio').on('blur', function() {
$("#myForm").validate().element( this );
});
});
请注意,您不要像这样调用模糊功能:
$('#check').on('blur', function() {
$("#myForm").validate().element( this );
}).blur();
初始模糊功能将导致验证在加载时触发
答案 1 :(得分:2)
onfocusout不显示错误消息,否则验证模糊元素的最佳方法是;
onfocusout: function(element) {
if( $(element).attr('name') ) {
this.element(element);
}
}
答案 2 :(得分:1)
检查onfocusout选项。
"Validate elements (except checkboxes/radio buttons) on blur. If nothing is entered, all rules are skipped, except when the field was already marked as invalid."