Resetting a multi-stage form with jQuery

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

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


Trouble i

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

    basically none of the provided solutions makes me happy. as pointed out by a few people they empty the form instead of resetting it.

    however, there are a few javascript properties that help out:

    • defaultValue for text fields
    • defaultChecked for checkboxes and radio buttons
    • defaultSelected for select options

    these store the value that a field had when the page was loaded.

    writing a jQuery plugin is now trivial: (for the impatient... here's a demo http://jsfiddle.net/kritzikratzi/N8fEF/1/)

    plugin-code

    (function( $ ){
        $.fn.resetValue = function() {  
            return this.each(function() {
                var $this = $(this); 
                var node = this.nodeName.toLowerCase(); 
                var type = $this.attr( "type" ); 
    
                if( node == "input" && ( type == "text" || type == "password" ) ){
                    this.value = this.defaultValue; 
                }
                else if( node == "input" && ( type == "radio" || type == "checkbox" ) ){
                    this.checked = this.defaultChecked; 
                }
                else if( node == "input" && ( type == "button" || type == "submit" || type="reset" ) ){ 
                    // we really don't care 
                }
                else if( node == "select" ){
                    this.selectedIndex = $this.find( "option" ).filter( function(){
                        return this.defaultSelected == true; 
                    } ).index();
                }
                else if( node == "textarea" ){
                    this.value = this.defaultValue; 
                }
                // not good... unknown element, guess around
                else if( this.hasOwnProperty( "defaultValue" ) ){
                    this.value = this.defaultValue; 
                }
                else{
                    // panic! must be some html5 crazyness
                }
            });
        }
    } )(jQuery);
    

    usage

    // reset a bunch of fields
    $( "#input1, #input2, #select1" ).resetValue(); 
    
    // reset a group of radio buttons
    $( "input[name=myRadioGroup]" ).resetValue(); 
    
    // reset all fields in a certain container
    $( "#someContainer :input" ).resetValue(); 
    
    // reset all fields
    $( ":input" ).resetValue(); 
    
    // note that resetting all fields is better with the javascript-builtin command: 
    $( "#myForm" ).get(0).reset(); 
    

    some notes ...

    • i have not looked into the new html5 form elements, some might need special treatment but the same idea should work.
    • elements need to be referenced directly. i.e. $( "#container" ).resetValue() won't work. always use $( "#container :input" ) instead.
    • as mentioned above, here is a demo: http://jsfiddle.net/kritzikratzi/N8fEF/1/
    0 讨论(0)
  • 2020-11-22 01:32

    I made a slight variation of Francis Lewis' nice solution. What his solution doesn't do is set the drop-down selects to blank. (I think when most people want to "clear", they probably want to make all values empty.) This one does it with .find('select').prop("selectedIndex", -1).

    $.fn.clear = function()
    {
        $(this).find('input')
                .filter(':text, :password, :file').val('')
                .end()
                .filter(':checkbox, :radio')
                    .removeAttr('checked')
                .end()
            .end()
        .find('textarea').val('')
            .end()
        .find('select').prop("selectedIndex", -1)
            .find('option:selected').removeAttr('selected')
        ;
        return this;
    };
    
    0 讨论(0)
  • 2020-11-22 01:32

    I'm using Paolo Bergantino solution which is great but with few tweaks... Specifically to work with the form name instead an id.

    For example:

    function jqResetForm(form){
       $(':input','form[name='+form+']')
       .not(':button, :submit, :reset, :hidden')
       .val('')
       .removeAttr('checked')
       .removeAttr('selected');
    }
    

    Now when I want to use it a could do

    <span class="button" onclick="jqResetForm('formName')">Reset</span>
    

    As you see, this work with any form, and because I'm using a css style to create the button the page will not refresh when clicked. Once again thanks Paolo for your input. The only problem is if I have defaults values in the form.

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

    Paolo's answer doesn't account for date pickers, add this in:

    $form.find('input[type="date"]').val('');
    
    0 讨论(0)
  • 2020-11-22 01:38

    I used the solution below and it worked for me (mixing traditional javascript with jQuery)

    $("#myformId").submit(function() {
        comand="window.document."+$(this).attr('name')+".reset()";
        setTimeout("eval(comando)",4000);
    })
    
    0 讨论(0)
  • 2020-11-22 01:40

    From http://groups.google.com/group/jquery-dev/msg/2e0b7435a864beea:

    $('#myform')[0].reset();
    

    setting myinput.val('') might not emulate "reset" 100% if you have an input like this:

    <input name="percent" value="50"/>
    

    Eg calling myinput.val('') on an input with a default value of 50 would set it to an empty string, whereas calling myform.reset() would reset it to its initial value of 50.

    0 讨论(0)
提交回复
热议问题