简体   繁体   中英

jQuery Validate - Remove Error Class from Parent Div

I have a set of checkboxes, that when none are selected, a class is added to the parent div to make the text red. However, when a box is selected, the class should be removed. With my current code, the class remains attached to the div when a checkbox is selected.

How do I remove the class once a single checkbox is selected?

$().ready(function() {

    $('#addForm').validate({
        rules: { 
            "items": { 
                required: true, 
                minlength: 1 
            } 
        }, 
        errorPlacement: function(error, element) {
            if (element.is(':checkbox')) {
                $(element).parent("div").addClass('checkbox-error');
            }
            else {
                 return true;
            }
         }
    }); 


});

Bind an event listener to the checkbox, which removes the class once it's checked:

$("#addForm :checkbox").change(function(){
    if(this.checked) {
        $(this).parent("div").removeClass("checkbox-error");
    }
})

您可以在复选框更改事件中执行此操作。

$("yourdiv").removeClass("checkbox-error");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM