Resetting a multi-stage form with jQuery

前端 未结 30 2065
谎友^
谎友^ 2020-11-22 00:58

I have a form with a standard reset button coded thusly:


Trouble i

30条回答
  •  青春惊慌失措
    2020-11-22 01:22

    I made a little improvement on Paolo Bergantino's original answer

    function resetFormInputs(context) {
        jQuery(':input', context)
        .removeAttr('checked')
        .removeAttr('selected')
        .not(':button, :submit, :reset, :hidden')
        .each(function(){
             jQuery(this).val(jQuery(this).prop('defautValue'));
        });
    }
    

    In this way, I can pass any context element to the function. I am able to reset the entire form or only a certain set of fields, for example:

    resetFormInputs('#form-id'); // entire form
    resetFormInputs('.personal-info'); // only the personal info field set
    

    Plus, the default values of the inputs are retained.

提交回复
热议问题