Resetting a multi-stage form with jQuery

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

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


Trouble i

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

    You might find that this is actually easier solved without jQuery.

    In regular JavaScript, this is as simple as:

    document.getElementById('frmitem').reset();
    

    I try to always remember that while we use jQuery to enhance and speed up our coding, sometimes it isn't actually faster. In those cases, it's often better to use another method.

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

    A method I used on a fairly large form (50+ fields) was to just reload the form with AJAX, basically making a call back to the server and just returning the fields with their default values. This made is much easier than trying to grab each field with JS and then setting it to it's default value. It also allowed to me to keep the default values in one place--the server's code. On this site, there were also some different defaults depending on the settings for the account and therefore I didn't have to worry about sending these to JS. The only small issue I had to deal with were some suggest fields that required initialization after the AJAX call, but not a big deal.

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

    document.getElementById('frm').reset()

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

    Modification of the most-voted answer for the $(document).ready() situation:

    $('button[type="reset"]').click(function(e) {
        $form = $(this.form);
        $form.find('input:text, input:password, input:file, select, textarea').val('');
        $form.find('input:radio, input:checkbox').removeAttr('checked').removeAttr('selected');
        e.preventDefault();
    });
    
    0 讨论(0)
  • 2020-11-22 01:20

    Simply use the jQuery Trigger event like so:

    $('form').trigger("reset");
    

    This will reset checkboxes, radiobuttons, textboxes, etc... Essentially it turns your form to it's default state. Simply put the #ID, Class, element inside the jQuery selector.

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

    I was having the same problem and the post of Paolo helped me out, but I needed to adjust one thing. My form with id advancedindexsearch only contained input fields and gets the values from a session. For some reason the following did not work for me:

    $("#advancedindexsearch").find("input:text").val("");
    

    If I put an alert after this, I saw the values where removed correctly but afterwards they where replaced again. I still don't know how or why but the following line did do the trick for me:

    $("#advancedindexsearch").find("input:text").attr("value","");
    
    0 讨论(0)
提交回复
热议问题