Clear form fields with jQuery

前端 未结 30 2710
甜味超标
甜味超标 2020-11-22 15:48

I want to clear all input and textarea fields in a form. It works like the following when using an input button with the reset class:

$(\".reset         


        
相关标签:
30条回答
  • 2020-11-22 16:18

    With Javascript you can simply do it with this syntax getElementById("your-form-id").reset();

    you can also use jquery by calling the reset function this way $('#your-form-id')[0].reset();

    Remember not to forget [0]. You will get the following error if

    TypeError: $(...).reset is not a function

    JQuery also provides an event you can use

    $('#form_id').trigger("reset");

    I tried and it works.

    Note: Its important to notice that these methods only reset your form to their initial value set by the server on page load. This means if your input was set on the value 'set value' before you did a random change, the field will be reset to that same value after reset method is called.

    Hope it helps

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

    I've written a universal jQuery plugin:

    /**
     * Resets any input field or form
     */
    $.fn.uReset = function () {
        return this.filter('form, :input').each(function () {
            var input = $(this);
            
            // Reset the form.
            if (input.is('form')) {
                input[0].reset();
                return;
            }
    
            // Reset any form field.
            if (input.is(':radio, :checkbox')) {
                input.prop('checked', this.defaultChecked);
            } else if (input.is('select')) {
                input.find('option').each(function () {
                    $(this).prop('selected', this.defaultSelected);
                });
            } else if (this.defaultValue) {
                input.val(this.defaultValue);
            } else {
                console.log('Cannot reset to default value');
            }
        });
    };
    
    $(function () {
        // Test jQuery plugin.
        $('button').click(function (e) {
            e.preventDefault();
            
            var button = $(this),
                inputType = button.val(),
                form = button.closest('form');
            
            if (inputType === 'form') {
                form.uReset()
            } else {
                $('input[type=' + inputType + '], ' + inputType, form).uReset();
            }
        });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <h3>Form</h3>
    <form>
        <input type="text" value="default"/><br /><br />
        Ch 1 (default checked) <input type="checkbox" name="color" value="1" checked="checked" /><br />
        Ch 2 <input type="checkbox" name="color" value="2" /><br />
        Ch 3 (default checked) <input type="checkbox" name="color" value="3" checked="checked" /><br /><br />
        <select name="time"><br />
            <option value="15">15</option>
            <option selected="selected" value="30">30</option>
            <option value="45">45</option>
        </select><br /><br />
        R 1 <input type="radio" name="color" value="1" /><br />
        R 2 (default checked) <input type="radio" name="color" value="2" checked="checked" /><br />
        R 3 <input type="radio" name="color" value="3" /><br /><br />
        <textarea>Default text</textarea><br /><br />
        
        <p>Play with form values and then try to reset them</p>
        
        <button type="button" value="text">Reset text input</button>
        <button type="button" value="checkbox">Reset checkboxes</button>
        <button type="button" value="select">Reset select</button>
        <button type="button" value="radio">Reset radios</button>
        <button type="button" value="textarea">Reset textarea</button>
        <button type="button" value="form">Reset the Form</button>
    </form>

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

    Why does it need to be done with any JavaScript at all?

    <form>
        <!-- snip -->
        <input type="reset" value="Reset"/>
    </form>
    

    http://www.w3.org/TR/html5/the-input-element.html#attr-input-type-keywords


    Tried that one first, it won't clear fields with default values.

    Here's a way to do it with jQuery, then:

    $('.reset').on('click', function() {
        $(this).closest('form').find('input[type=text], textarea').val('');
    });
    
    0 讨论(0)
  • 2020-11-22 16:20
    $('#editPOIForm').each(function(){ 
        this.reset();
    });
    

    where editPOIForm is the id attribute of your form.

    0 讨论(0)
  • 2020-11-22 16:21
    @using (Ajax.BeginForm("Create", "AcceptanceQualityDefect", new AjaxOptions()
    {
        OnSuccess = "ClearInput",
        HttpMethod = "Post",
        UpdateTargetId = "defect-list",
        InsertionMode = InsertionMode.Replace
    }, new { @id = "frmID" })) 
    
    1. frmID is the identification of the form
    2. OnSuccess of the operation we call the JavaScript function with the name "ClearInput"

      <script type="text/javascript">
          function ClearInput() {
              //call action for render create view
              $("#frmID").get(0).reset();
          }
      </script>
      
    3. if you do both of these right, then you will not be able to stop it from working...

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

    The following code clear all the form and it's fields will be empty. If you want to clear only a particular form if the page is having more than one form, please mention the id or class of the form

    $("body").find('form').find('input,  textarea').val('');
    
    0 讨论(0)
提交回复
热议问题