Is there a function in jquery.validate that will reset a single field like resetForm does for a form?

后端 未结 6 2352
旧巷少年郎
旧巷少年郎 2021-02-18 15:59

I want to call a jquery function to manually remove errors from a single field and reset the error markup. Is there a function that does this that would be similar to the reset

6条回答
  •  南方客
    南方客 (楼主)
    2021-02-18 16:03

    This functionality is possible via focusCleanup: true option and triggering focusin event on desired element. But it is not always desirable to trigger this event. So apparently, you can use plugin’s internal code which is running on focusin and invoke it externally like so:

    /*get your element and containing validated form*/
    var $that = $('#desired_elemenet');
    var $form = $that.closest('form');
    
    /*get validator object for the form*/
    var validator = $form.validate();
    
    /*use internal code of a plugin to clear the field*/
    if (validator.settings.unhighlight) {
        validator.settings.unhighlight.call( validator, $that[0], validator.settings.errorClass, validator.settings.validClass );
    }
    validator.hideThese( validator.errorsFor( $that[0] ) );
    

提交回复
热议问题