Clear form fields with jQuery

前端 未结 30 2764
甜味超标
甜味超标 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:12

    I got easiest trick to reset form

    jQuery("#review-form")[0].reset();
    

    or

    $("#review-form").get().reset();
    
    0 讨论(0)
  • 2020-11-22 16:12

    Why you dont use document.getElementById("myId").reset(); ? this is the simple and pretty

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

    For jQuery 1.6+:

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

    For jQuery < 1.6:

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

    Please see this post: Resetting a multi-stage form with jQuery

    Or

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

    As jQuery suggests:

    To retrieve and change DOM properties such as the checked, selected, or disabled state of form elements, use the .prop() method.

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

    If someone is still reading this thread, here is the simplest solution using not jQuery, but plain JavaScript. If your input fields are inside a form, there is a simple JavaScript reset command:

    document.getElementById("myform").reset();
    

    More about it here: http://www.w3schools.com/jsref/met_form_reset.asp

    Cheers!

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

    Use this Code Where you want to Call Normal Reset Function by jQuery

    setTimeout("reset_form()",2000);
    

    And Write this Function Out Site jQuery on Document Ready

    <script>
    function reset_form()
    {
        var fm=document.getElementById('form1');
        fm.reset();
    }
    </script>
    
    0 讨论(0)
  • 2020-11-22 16:18

    This won't handle cases where form input fields have non empty default values.

    Something like should work

    $('yourdiv').find('form')[0].reset();
    
    0 讨论(0)
提交回复
热议问题