简体   繁体   中英

How to clear all input fields after submitting the form?

I am using this function for posting my form :

$.ajax({
    type: 'POST',
    url: ".",
    data: {'postdata':allVals,
                   csrfmiddlewaretoken: '{{ csrf_token }}'},
    success: function(responseText) {
        console.log("Posted Data");
    }
});

This function is called on a button click event which then posts the data. Now please tell me how can I clear all the input fields as soon as the data is posted successfully. Please help!

Try...

$('form')[0].reset();

...inside your success callback.

Try this...call this in success

 success: function(responseText) {
            $('#form input').attr('value','');//        console.log("Posted Data");
    }

You can add this code into your success method.

success: function(responseText) {
    console.log("Posted Data");
    $('#yourform :input').val('');
}
$(':input','#myform')
    .not(':button, :submit, :reset, :hidden')
    .val('')
    .removeAttr('checked')
    .removeAttr('selected');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM