Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Clear Form on close

I have the below JQuery Dialog script, I'm trying to find out how to fire off a function that clears the form when I close the dialog.

function clearForm()
{
$(':input','#calcQuery')
.not(':button, :submit, :reset, :hidden')
.val('');
};
// form popup 
$(document).ready(function() 
{
//var dataString = $("#calcQuery").serialize();
    $("#formBox").dialog({
      bgiframe: true,
        autoOpen: false, 
        height: 600,
        width: 400, 
        modal: false,
        closeOnEscape: true,
        title: "Calculator",
        buttons:    {
            "Calculate": function() {

// form post
            $.ajax({
            type: "POST",
            url: "calc.php",
            data: $("#calcQuery").serialize(),
            dataType: "html",
            success: function(response)
                {
                $("#calcBox").html(response);
                $("#calcBox").show();   
                },
            error: function
                (xhr, ajaxOptions, thrownError)
                    {
                    alert(xhr.status); 
                    alert(thrownError);
                    }



    }).responseText;

// form post 

                }
            } 
    });

$('#calcButton').click(function(){
    $('#formBox').dialog('open');
    return false;
    });

});

$("#formBox").bind('dialogclose', function(event)
{
clearForm();
}); 
like image 850
Simply Seth Avatar asked Dec 07 '09 15:12

Simply Seth


People also ask

How do you clear form data after submit in jQuery?

Answer: Use the reset() Method.

How do you clear form fields?

To clear all the input in an HTML form, use the <input> tag with the type attribute as reset.

How can we make all fields empty in jQuery?

bind("click", function() { $("input[type=text], textarea"). val(""); }); This will clear all fields on the page, not just the ones from the form.

How do I reset form data?

reset() method restores a form element's default values. This method does the same thing as clicking the form's <input type="reset"> control. If a form control (such as a reset button) has a name or id of reset it will mask the form's reset method. It does not reset other attributes in the input, such as disabled .


1 Answers

This will reset the form:

$("#form").trigger( "reset" );
like image 116
develop-ua Avatar answered Nov 15 '22 05:11

develop-ua