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
- $ (Document). Ready (function (){
- VaR Options = {
- Target: '# mydiv', // the region to be refreshed
- // Beforesubmit: showrequest, // method called before submission
- // Success: showresponse // method of the returned result
- // Other available options:
- // URL: URL // submitted URL. By default, form action is used.
- // Type: Type // 'get' or 'post', override for form's 'method' attribute
- // Datatype: NULL // 'xml', 'script', or 'json' (expected server response type)
- // Clearform: True // whether to clear the form
- // Resetform: True // whether to reset form
- // $. Ajax options can be used here too, for example:
- // Timeout: 3000
- };
- // Bind the form to submit the event
- $ ('# Myform'). Submit (function (){
- $ (This). ajaxsubmit (options );
- //!!! Important !!!
- // Always return false to prevent standard browser submit and page navigation
- Return false;
- });
- });
Before and after the call:
Java code
- // Pre-submit callback
- Function showrequest (formdata, jqform, options ){
- // Formdata is an array; here we use $. Param to convert it to a string to display it
- // But the form plugin does this for you automatically when it submits the data
- VaR querystring = $. Param (formdata );
- // Jqform is a jquery object encapsulating the form element. To access
- // Dom element for the form do this:
- // Var formelement = jqform [0];
- Alert ('About to submit: \ n \ n' + querystring );
- // Here We cocould return false to prevent the form from being submitted;
- // Returning anything other than false will allow the form submit to continue
- Return true;
- }
- // Post-submit callback
- Function showresponse (responsetext, statustext ){
- // For normal HTML responses, the first argument to the success callback
- // Is the XMLHTTPRequest object's responsetext Property
- // If the ajaxsubmit method was passed an options object with the datatype
- // Property set to 'xml' then the first argument to the success callback
- // Is the XMLHTTPRequest object's responsexml Property
- // If the ajaxsubmit method was passed an options object with the datatype
- // Property set to 'json' then the first argument to the success callback
- // Is the JSON data object returned by the server
- Alert ('status: '+ statustext +' \ n \ nresponsetext: \ n' + responsetext +
- '\ N \ nthe output Div shoshould have already been updated with the responsetext .');
- }
You can write a public method in the project:
Java code
- // Partial submission form
- Function formsubmit (formid, divid, URL ){
- $ ('#' + Formid). Submit (function (){
- $ (This). ajaxsubmit ({
- Target: '#' + divid,
- URL: URL,
- Error: function (){
- Alert ('Load page' + URL + 'error! ')
- }
- });
- Return false;
- });
- }
========================================================== ==================================
Case refresh table:
1. Separate the table into a JSP, and include the table page on the home page.
2. Home Page:
Java code
- Window. onload = function (){
- // Submit the form in Ajax to refresh the table
- $ ('# Queryform'). Submit (function (){
- $ (This). ajaxsubmit ({
- Target: '# table1'
- });
- Return false;
- });
- }
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
- /**
- * Ajax summary query the list of undisclosed Insiders
- * Department compliance risk control specialists collect and query
- */
- Public String ajaxgatherinsiderlist (){
- // Query related business data
- Return success;
- }