简体   繁体   中英

reset form fields after submit the form

Hello I m using jquery validations , my form is validating and submitting successfully but the fields containing the previous data, i want to reset the fields after submitting the form.

the $('#frm').reset(); is not working. $('#frm').reset(); is not working.

submitHandler: function(form)
{
    $.ajax({
        url: "contact_submit.php",
        type: "post",
        data: $("#frm").serialize(),
        success:function(response)                  
        {
            if(response)
            {
                $('#msg-valid').html('<img src="img/valid.png">Your form is submited, Thank You !');
                $('#frm').reset();
            }
            else{
                $('#msg-error').html('<img src="img/error.png">Your form is not submited');                         
            }
        }
    });
}

Basically .reset() is a native javascript function. You are trying to invoke it from wrapped Jquery object. So probabbly it would have caused error in your console , So in order to access it, first you have to extract the javascript object from the Jquery object then invoke that function like,

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

Try with

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

It will clear the form fields

I found that,the following code reset the form after submit.

submitHandler: function(form)
        {
            $.ajax({
                url: "contact_submit.php",
                type: "post",
                data: $("#frm").serialize(),
                success:function(response)

                {
                    if(response)
                    {
                    $('#msg-valid').html('<img src="img/valid.png">Your form is submited, Thank You !');

                        $(':input').not(':button, :submit, :reset, :hidden, :checkbox, :radio').val('');
                        $(':checkbox, :radio').prop('checked', false);

                    }
                    else{
                        $('#msg-error').html('<img src="img/error.png">Your form is not submited');

                    }
                }

            });

        }

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