1

使用 jquery 验证器。使用选项卡导航表单时不执行验证。

这是小提琴

$(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!",
             }, 
         }); 
     });

我试图在复选框上执行模糊处理。但是,该事件是在表单加载时触发的。这是改进的小提琴

$('#check').on('blur', function() {
            $("#myForm").validate().element( this );
        }).blur();
4

3 回答 3

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 );
    });
});

请注意,您不要像这样调用 blur 函数:

$('#check').on('blur', function() {
    $("#myForm").validate().element( this );
}).blur();

初始模糊功能将导致验证在加载时触发

这是工作小提琴

于 2013-07-03T16:29:56.237 回答
2

onfocusout 不显示错误消息,否则验证模糊元素的最佳方法是;

onfocusout: function(element) { 
    if( $(element).attr('name') ) { 
            this.element(element);  
    }
}
于 2017-12-14T16:21:47.650 回答
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."

证实

于 2013-05-29T10:16:41.040 回答