复选框的jquery验证消息不起作用

时间:2018-09-12 07:53:03

标签: jquery codeigniter validation checkbox message

如果未选中任何复选框,我需要获得一条消息,消息为“ 此字段必填”。

查看:

<div style="width: 100%;">
                <label class="headlabel" id="passLable" name="passLable">Category List : <strong>*</strong></label>
                <div style="width: 100%;" class="form-group px-3">
                    <?php foreach ($cat as $data2) { ?>
                        <div class="form-check px-2" style="display: inline-block; margin-right: 20px;">
                            <input type="checkbox" class="form-check-input" value="<?php echo $data2->id ?>" name="cat[]" id="category">
                            <label class="form-check-label"
                                   style="padding-left: 10px;"><?php echo $this->lang->line($data2->cat_name); ?> </label>
                        </div>
                    <?php } ?>
                </div>
            </div>

js:

$(document).ready(function () {
   $('#addNewCompanyForm').each(function(){
    $(this).validate({
        submitHandler : function(){
            addNewCompany();
        },
        rules: {    
            'cat[]' : {
                required : true
            }
        },            

        messages: {                
             'cat[]' : {
                 required : "plase fill this."
             }
        },
        errorElement: "small",
        errorPlacement: function ( error, element ) {
            error.addClass( "form-control-feedback" );

            element.parents( ".form-group" ).addClass( "form-control-feedback" );

            if ( element.prop( "type" ) === "checkbox" ) {
                error.insertAfter( element.parent( "label" ) );
            } else {
                error.insertAfter( element );
            }
        },
        highlight: function ( element, errorClass, validClass ) {
            $( element ).parents( ".form-group" ).addClass( "has-danger" ).removeClass( "has-success" );
            $('input').addClass( "form-control-danger" ).removeClass( "form-control-success" );
        },
        unhighlight: function ( element, errorClass, validClass ) {
            $( element ).parents( ".form-group" ).addClass( "has-success" ).removeClass( "has-danger" );
            $('input').addClass( "form-control-success" ).removeClass( "form-control-danger" );
        }
    });
});
});

这已经以红色显示了复选框,但未显示任何消息。如何获取验证消息。

我在上面添加了完整的js代码。

修改

enter image description here

1 个答案:

答案 0 :(得分:1)

在foreach循环下面添加跨度

<div style="width: 100%;">
            <label class="headlabel" id="passLable" name="passLable">Category List : <strong>*</strong></label>
            <div style="width: 100%;" class="form-group px-3">
                <?php foreach ($cat as $data2) { ?>
                    <div class="form-check px-2" style="display: inline-block; margin-right: 20px;">
                        <input type="checkbox" class="form-check-input" value="<?php echo $data2->id ?>" name="cat[]" id="category">
                        <label class="form-check-label"
                               style="padding-left: 10px;"><?php echo $this->lang->line($data2->cat_name); ?> </label>
                    </div>
                <?php } ?>
                <span class="error" id='errorCheck'></span>
            </div>
        </div>

使用$('#errorCheck').html(error)

 $(document).ready(function () {
$('#addNewCompanyForm').validate({
    submitHandler : function(){
        addNewCompany();
    },
    rules: {    
        'cat[]' : {
            required : true
        }
    },            

    messages: {                
         'cat[]' : {
             required : "plase fill this."
         }
    },
    errorElement: "small",
    errorPlacement: function ( error, element ) {
        error.addClass( "form-control-feedback" );

        element.parents( ".form-group" ).addClass( "form-control-feedback" );

        if ( element.hasClass( "form-check-input" )) {
            $('#errorCheck').html(error);
        } else {
            error.insertAfter( element );
        }
    },
    highlight: function ( element, errorClass, validClass ) {
        $( element ).parents( ".form-group" ).addClass( "has-danger" ).removeClass( "has-success" );
        $('input').addClass( "form-control-danger" ).removeClass( "form-control-success" );
    },
    unhighlight: function ( element, errorClass, validClass ) {
        $( element ).parents( ".form-group" ).addClass( "has-success" ).removeClass( "has-danger" );
        $('input').addClass( "form-control-success" ).removeClass( "form-control-danger" );
    }
});
});