JQuery implements the Ajax form submission program

Source: Internet
Author: User


Description: This section describes how to quickly convert a common form into an ajax-based submission and display the result in the dialog box.
Body:
We have a very common form:
 
<Form id = "form1" name = "form1" method = "get" action = "post.html">
Title <input id = "testtitle" name = "testtitle" type = "text" size = "40"/>
<Input type = "submit" value = "submit">
</Form>
For ease of demonstration, the method uses get and can be changed to post as needed.
 
Now Let's transform it into AJAX-based submission. The method is simple. Just copy the following code to the page:
 
<Link type = "text/css" href = "jquery-ui.css" rel = "stylesheet"/>
<Style>
# Loading {background-image: url (images/loading.gif); background-position: 0px 0px; background-repeat: no-repeat; position: absolute; width: 50px; height: 50px; top: 60%; left: 50%; margin-left:-25px; text-align: center ;}
</Style>
<Script type = "text/javascript" src = "jquery. js"> </script>
<Script type = "text/javascript" src = "jquery. form. js"> </script>
<Script type = "text/javascript" src = "jquery-ui.js"> </script>
<Script type = "text/javascript">
$ (Function (){
BindSubmit ();
$ ("# Loading"). hide ();
$ ("# Msgdlg"). hide ();
});

Function bindSubmit (){
Var options = {
Target: '# msgdlg ',
Success: showResponse,
Error: showError

// Other optional parameters:
// Url: url // override for form's 'action' attribute
// Type: type // 'get' or 'post', override for form's 'method' attribute
// DataType: null // 'xml', 'script', or 'json' (expected server response type)
// ClearForm: true // clear all form fields after successful submit
// ResetForm: true // reset the form after successful submit

// $. Ajax options, such as timeout:
// Timeout: 3000
};

$ ('# Form1'). submit (function (){
$ (This). ajaxSubmit (options );
$ ("# Loading"). show ();
Return false;
});
}

// Callback function for successful response
Function showResponse (responseText, statusText, xhr, $ form ){
$ ("# Loading"). hide ();
Messagebox (responseText );
}

// Response failed
Function showError (xhr, ajaxOptions, thrownError ){
$ ("# Loading"). hide ();
Messagebox ("error! "+ ThrownError );
}

// Displayed result dialog box
Function messagebox (msg ){
$ ("# Msgdlg" cmd.html (msg );
$ ("# Dialog: ui-dialog"). dialog ("destroy ");
$ ("# Msgdlg"). dialog ({
Modal: true,
Width: 380,
Height: 230,
Buttons :{
Confirm: function (){
$ (This). dialog ("close ");
                }
            }
});
}
</Script>

<Div id = "msgdlg" title = "Message"> </div>
<Div id = "loading" style = "display: none" ondblclick = "this. style. display = 'none'"> </div>
Key points in the code:
(1) $ ('# form1'). submit () binds the Form Submit (jquery. form. js is used)
(2) the showResponse function is defined to process the returned information.
(3) $ ("# msgdlg"). dialog () create dialog box and display the result (with jquery-ui.js)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.