Clear form fields with jQuery

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

    Simple but works like a charm.

    $("#form").trigger('reset'); //jquery
    document.getElementById("myform").reset(); //native JS
    
    0 讨论(0)
  • 2020-11-22 16:36
    $('form[name="myform"]')[0].reset();
    
    0 讨论(0)
  • 2020-11-22 16:36

    Add hidden reset button as follows

    <input id="resetBtn" type="reset" value="reset" style="display:none" />
    // Call reset buttons click event
    // Similar to ClearInputs('resetBtn');
    function ClearInputs(btnSelector) {
         var btn = $("#" + btnSelector);
         btn.click();
    }
    
    0 讨论(0)
  • 2020-11-22 16:38

    if you use selectors and make values to empty values, it is not resetting the form, it's making all fields empty. Reset is to make form as it was before any edit actions from user after the load of form from server side. If there is an input with name "username" and that username was prefilled from server side, most of solutions on this page will delete that value from input, not reset it to the value how it was before user's changes. If you need to reset the form, use this:

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

    if you need not to reset the form, but fill all inputs with some value, for example empty value, then you can use most of the solutions from other comments.

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

    If you want to empty all input boxes irrespective of its type then it's a minute step by

     $('#MyFormId')[0].reset();
    
    0 讨论(0)
  • 2020-11-22 16:39

    $('form').submit(function() {

    var el = $(this);
    
    $('<button type="reset" style="display:none; "></button>')
        .appendTo(el)
        .click()
        .remove()
    ;
    
    return false;
    

    });

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