Jquery Ajax form submission implements partial refresh ajaxsubmit

Source: Internet
Author: User

Jquery ajaxsubmit can be used to partially refresh the page Div for Ajax submitted forms (it can be used to refresh JSP tables !)

 

Need to introduce: jquery-form.js

 

 

Instructions for use:

 

Java code

  1. $ (Document). Ready (function (){
  2. VaR Options = {
  3. Target: '# mydiv', // the region to be refreshed
  4. // Beforesubmit: showrequest, // method called before submission
  5. // Success: showresponse // method of the returned result
  6. // Other available options:
  7. // URL: URL // submitted URL. By default, form action is used.
  8. // Type: Type // 'get' or 'post', override for form's 'method' attribute
  9. // Datatype: NULL // 'xml', 'script', or 'json' (expected server response type)
  10. // Clearform: True // whether to clear the form
  11. // Resetform: True // whether to reset form
  12. // $. Ajax options can be used here too, for example:
  13. // Timeout: 3000
  14. };
  15. // Bind the form to submit the event
  16. $ ('# Myform'). Submit (function (){
  17. $ (This). ajaxsubmit (options );
  18. //!!! Important !!!
  19. // Always return false to prevent standard browser submit and page navigation
  20. Return false;
  21. });
  22. });

 

 

Before and after the call:

 

Java code

  1. // Pre-submit callback
  2. Function showrequest (formdata, jqform, options ){
  3. // Formdata is an array; here we use $. Param to convert it to a string to display it
  4. // But the form plugin does this for you automatically when it submits the data
  5. VaR querystring = $. Param (formdata );
  6. // Jqform is a jquery object encapsulating the form element. To access
  7. // Dom element for the form do this:
  8. // Var formelement = jqform [0];
  9. Alert ('About to submit: \ n \ n' + querystring );
  10. // Here We cocould return false to prevent the form from being submitted;
  11. // Returning anything other than false will allow the form submit to continue
  12. Return true;
  13. }
  14. // Post-submit callback
  15. Function showresponse (responsetext, statustext ){
  16. // For normal HTML responses, the first argument to the success callback
  17. // Is the XMLHTTPRequest object's responsetext Property
  18. // If the ajaxsubmit method was passed an options object with the datatype
  19. // Property set to 'xml' then the first argument to the success callback
  20. // Is the XMLHTTPRequest object's responsexml Property
  21. // If the ajaxsubmit method was passed an options object with the datatype
  22. // Property set to 'json' then the first argument to the success callback
  23. // Is the JSON data object returned by the server
  24. Alert ('status: '+ statustext +' \ n \ nresponsetext: \ n' + responsetext +
  25. '\ N \ nthe output Div shoshould have already been updated with the responsetext .');
  26. }

 

 

You can write a public method in the project:

 

Java code

  1. // Partial submission form
  2. Function formsubmit (formid, divid, URL ){
  3. $ ('#' + Formid). Submit (function (){
  4. $ (This). ajaxsubmit ({
  5. Target: '#' + divid,
  6. URL: URL,
  7. Error: function (){
  8. Alert ('Load page' + URL + 'error! ')
  9. }
  10. });
  11. Return false;
  12. });
  13. }

 

 

========================================================== ==================================

Case refresh table:

 

1. Separate the table into a JSP, and include the table page on the home page.

 

2. Home Page:

 

 

Java code

  1. Window. onload = function (){
  2. // Submit the form in Ajax to refresh the table
  3. $ ('# Queryform'). Submit (function (){
  4. $ (This). ajaxsubmit ({
  5. Target: '# table1'
  6. });
  7. Return false;
  8. });
  9. }

 

Click the query button to submit the form.

 

3. Java: The method for submitting and calling form is the same as that for common actions. configure this action in struts to jump to the separate table JSP page. After the result is returned successfully, the table is refreshed.

 

Java code

  1. /**
  2. * Ajax summary query the list of undisclosed Insiders
  3. * Department compliance risk control specialists collect and query
  4. */
  5. Public String ajaxgatherinsiderlist (){
  6. // Query related business data
  7. Return success;
  8. }

 

 

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.