Kelbizzle
Kelbizzle

Reputation: 686

Clear form after submission with jQuery

What's the easiest way to clear this form after refresh. The way I have tried will clear the form but not submit to the database. Could someone else explain to me the best way to do this.

<html>
<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
    <script type="text/javascript">
    jQuery(document).ready(function($){
        $("#newsletterform").validate({
            debug: false,
            rules: {
                name: "required",
                email: {
                    required: true,
                    email: true
                }
            },
            messages: {
                name: "Please let us know who you are.",
                email: "A valid email will help us get in touch with you.",
            },
            submitHandler: function(form) {
                // do other stuff for a valid form
                $.post('newsletter.php', $("#newsletterform").serialize(), function(data) {
                    $('#results').html(data);
                });

            }

        });
    });

    </script>

</head>

<div id="content">
    <div id="newsletter-signup">
        <h1>Sign up for News, Updates, and Offers!</h1>
        <form id="newsletterform" action="" method="post">

            <fieldset>

                <ul>
                    <li>
                        <label for="name">Name:</label>
                        <input type="text" name="name" />
                    </li>
                    <li>
                        <label for="email">Email:</label>
                        <input type="text" name="email" />  
                    </li>
                        <div id="results"><div>
                    <li>
                        <input type="submit" value="submit" name="signup" onclick="" />         
                    </li>
                </ul>

            </fieldset>

        </form>         
    </div>
</div>
</html>

Upvotes: 32

Views: 168390

Answers (7)

Sachin Jeremiay
Sachin Jeremiay

Reputation: 514

A quick reset of the form fields is possible with this jQuery reset function. $(selector)[0].reset();

Upvotes: 0

Amritpal singh
Amritpal singh

Reputation: 865

You can reset your form with:

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

Upvotes: 38

kiran kumar
kiran kumar

Reputation: 1422

Better way to reset your form with jQuery is Simply trigger a reset event on your form.

$("#btn1").click(function () {
        $("form").trigger("reset");
    });

Upvotes: 15

Harshit Khatri
Harshit Khatri

Reputation: 5

Just add this to your Action file in some div or td, so that it comes with incoming XML object

    <script type="text/javascript">
    $("#formname").resetForm();
    </script>

Where "formname" is the id of form you want to edit

Upvotes: -3

mas-designs
mas-designs

Reputation: 7536

Propably this would do it for you.

 $('input').val('').removeAttr('checked').removeAttr('selected');

Upvotes: 1

Andrew Jackman
Andrew Jackman

Reputation: 13966

You can add this to the callback from $.post

$( '#newsletterform' ).each(function(){
    this.reset();
});

You can't just call $( '#newsletterform' ).reset() because .reset() is a form object and not a jquery object, or something to that effect. You can read more about it here about half way down the page.

Upvotes: 89

Dau
Dau

Reputation: 8848

try this in your post methods callback function

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

for more info read this

Upvotes: 1

Related Questions