Use Javascript asynchronous form submission, Image Upload, and compatibility with asynchronous ajax simulation technology. For more information, see. Preface:
I think the title is still quite long. There are many other functions. In fact, the simplification is just a function. asynchronous form submission only supports Image Upload under the big function of asynchronous form submission, simulate ajax technology (in fact, it was a long time ago that it was used to implement compatibility with ajax in multiple browsers. Here it is just a nostalgia for playing as a technology ), the following content must have a certain js Foundation, otherwise it will be difficult to understand.
Note:
This is a method in bBank. Now I extract it into a general method to explain it.
BBank framework: http://www.cnblogs.com/bruceli/archive/2010/04/15/bBank.html
Why do we need to mention bBank? In the following method, the evaluation of bBank array is used, and the standard dom conversion of string html is used (this is described in my previous article. Click here for details ), remove your own method and css selector. You can replace them with your own method, or directly use the bBank method. You can download bBank to view the source code.
B $. type. isArray (args) determines whether it is an array
B $. parseDom (strs) string is directly converted to a standard dom object
B $ ('# bBankAsynFormSubmit_form_1b'). removeSelf () Remove yourself
B $ () css Selector
Principle:
The core is to use iframe to complete Asynchronization. Put a hidden iframe in the page, and the form has a property target. Set the target as the iframe for asynchronous submission. When you submit the form, in fact, iframe is used as the carrier for submitting the display, and other content on the page is refreshing.
In fact, the principle is very simple. Since iframe is used as the display carrier, we can extend it to the following: if the submitted page has a returned value, the returned value will become the content in the body of iframe, whether the content in the body is obtained through iframe is a bit like ajax. This is the earliest method used to implement Asynchronization.
Ajax is time-triggered. To simulate ajax, we also need to trigger the event. In fact, it is very easy to use the iframe onload to load the event.
Implementation:
Code:
The Code is as follows:
Var asyn = {
FormSubmit: function (args, action, func ){
This. clearContext ();
This. callBack = null;
Var subArr = [];
Var subArrT = [];
If (B $. type. isArray (args )){
SubArr = args;
} Else {
Var tag = args. tagName. toLowerCase ();
If (tag = "form") {for (var I = 0, num = args. childNodes. length; I <num; I ++) {subArr. push (args. childNodes [I]) ;}}
Else {subArr = [args];}
}
// Create asyn form and ifroma
Var objForm = document. createElement ("form ");
ObjForm. action = action;
ObjForm.tar get = "bBankAsynFormSubmit_iframe_1b ";
ObjForm. encoding = "multipart/form-data ";
ObjForm. method = "post ";
ObjForm. id = "bBankAsynFormSubmit_form_1b ";
ObjForm. style. display = "none ";
Var objIframe = B $. parseDom ('') [0];
// Add submit value in form
For (var I = 0, num = subArr. length; I <num; I ++ ){
If (! SubArr [I]. name & subArr [I]. nodeType = 1 & subArr [I]. tagName. toLowerCase () = "input") subArr [I]. name = "bBankAsynFormSubmit_input_1b _" + I;
Var input = subArr [I]. cloneNode (true );
SubArrT. push (input );
SubArr [I]. parentNode. replaceChild (input, subArr [I]);
ObjForm. appendChild (subArr [I]);
}
// Submit
Document. body. appendChild (objIframe );
Document. body. appendChild (objForm );
ObjForm. submit ();
// Dispose
For (var I = 0, num = subArrT. length; I <num; I ++) {subArrT [I]. parentNode. replaceChild (subArr [I], subArrT [I]);}
If (func) this. callBack = func;
},
Complete: function (){
Var responseText = "";
Try {
Var objIframe = document. getElementById ("bBankAsynFormSubmit_iframe_1b ");
If (objIframe. contentWindow) {responseText = objiframe.content20.doc ument. body. innerHTML ;}
Else {responseText = objIframe.contentDocument.doc ument. body. innerHTML ;}
} Catch (err ){}
This. clearContext ();
If (this. callBack) this. callBack (responseText );
},
ClearContext: function (){
If (B $ ('# bBankAsynFormSubmit_form_1b') B $ ('# bBankAsynFormSubmit_form_1b'). removeSelf ();
If (B $ ('# bBankAsynFormSubmit_iframe_1b') B $ ('# bBankAsynFormSubmit_iframe_1b'). removeSelf ();
},
CallBack: null
};
Brief Introduction:
In fact, it is troublesome to solve the problem of multi-browser compatibility of iframe. In fact, we do not need to discuss the compatibility problem of iframe, we handed the compatibility to the browser for resolution and solution. Here we use string to convert dom.
First, I created a form and iframe, both of which are hidden. I transferred the content of the form to the hidden form I created and submitted the hidden form. After all, remove the created form and iframe.
Usage:
Asyn. formSubmit (args, action [, func]) arg: Can Be A form, an input form element, and an array of input form elements. Action: submitted url. Func: callback function
Example: asyn. formSubmit (inputArr, 'xxx. aspx ', function (d ){
Alert (d );
});
END
This is the end. Here we recommend a self-written js framework. The above method is integrated into the framework.
Usage:
The Code is as follows:
B $. asyn. formSubmit (inputArr, 'xxx. aspx ', function (d ){
Alert (d );
});