Resetting a multi-stage form with jQuery

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

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


Trouble i

相关标签:
30条回答
  • 2020-11-22 01:15

    updated on March 2012.

    So, two years after I originally answered this question I come back to see that it has pretty much turned into a big mess. I feel it's about time I come back to it and make my answer truly correct since it is the most upvoted + accepted.

    For the record, Titi's answer is wrong as it is not what the original poster asked for - it is correct that it is possible to reset a form using the native reset() method, but this question is trying to clear a form off of remembered values that would remain in the form if you reset it this way. This is why a "manual" reset is needed. I assume most people ended up in this question from a Google search and are truly looking for the reset() method, but it does not work for the specific case the OP is talking about.

    My original answer was this:

    // not correct, use answer below
    $(':input','#myform')
    .not(':button, :submit, :reset, :hidden')
    .val('')
    .removeAttr('checked')
    .removeAttr('selected');
    

    Which might work for a lot of cases, including for the OP, but as pointed out in the comments and in other answers, will clear radio/checkbox elements from any value attributes.

    A more correct answer (but not perfect) is:

    function resetForm($form) {
        $form.find('input:text, input:password, input:file, select, textarea').val('');
        $form.find('input:radio, input:checkbox')
             .removeAttr('checked').removeAttr('selected');
    }
    
    // to call, use:
    resetForm($('#myform')); // by id, recommended
    resetForm($('form[name=myName]')); // by name
    

    Using the :text, :radio, etc. selectors by themselves is considered bad practice by jQuery as they end up evaluating to *:text which makes it take much longer than it should. I do prefer the whitelist approach and wish I had used it in my original answer. Anyhow, by specifying the input part of the selector, plus the cache of the form element, this should make it the best performing answer here.

    This answer might still have some flaws if people's default for select elements is not an option that has a blank value, but it is certainly as generic as it is going to get and this would need to be handled on a case-by-case basis.

    0 讨论(0)
  • 2020-11-22 01:15

    All these answers are good but the absolute easiest way of doing it is with a fake reset, that is you use a link and a reset button.

    Just add some CSS to hide your real reset button.

    input[type=reset] { visibility:hidden; height:0; padding:0;}
    

    And then on your link you add as follows

    <a href="javascript:{}" onclick="reset.click()">Reset form</a>
    
    <input type="reset" name="reset" id="reset" /><!--This input button is hidden-->
    

    Hope this helps! A.

    0 讨论(0)
  • 2020-11-22 01:16

    There's a big problem with Paolo's accepted answer. Consider:

    $(':input','#myform')
     .not(':button, :submit, :reset, :hidden')
     .val('')
     .removeAttr('checked')
     .removeAttr('selected');
    

    The .val('') line will also clear any value's assigned to checkboxes and radio buttons. So if (like me) you do something like this:

    <input type="checkbox" name="list[]" value="one" />
    <input type="checkbox" name="list[]" value="two" checked="checked" />
    <input type="checkbox" name="list[]" value="three" />
    

    Using the accepted answer will transform your inputs into:

    <input type="checkbox" name="list[]" value="" />
    <input type="checkbox" name="list[]" value="" />
    <input type="checkbox" name="list[]" value="" />
    

    Oops - I was using that value!

    Here's a modified version that will keep your checkbox and radio values:

    // Use a whitelist of fields to minimize unintended side effects.
    $('INPUT:text, INPUT:password, INPUT:file, SELECT, TEXTAREA', '#myFormId').val('');  
    // De-select any checkboxes, radios and drop-down menus
    $('INPUT:checkbox, INPUT:radio', '#myFormId').removeAttr('checked').removeAttr('selected');
    
    0 讨论(0)
  • 2020-11-22 01:17

    Clearing forms is a bit tricky and not as simple as it looks.

    Suggest you use the jQuery form plugin and use its clearForm or resetForm functionality. It takes care of most of the corner cases.

    0 讨论(0)
  • 2020-11-22 01:17

    I usually do:

    $('#formDiv form').get(0).reset()
    

    or

    $('#formId').get(0).reset()
    
    0 讨论(0)
  • 2020-11-22 01:17

    Consider using the validation plugin - it's great! And reseting form is simple:

    var validator = $("#myform").validate();
    validator.resetForm();
    
    0 讨论(0)
提交回复
热议问题