Sample Code for Ajax to automatically obtain the value of a form Element Through JS Code, ajaxjs
When using Ajax, we usually need to obtain the value of the form element and send it to the backend server for processing. If there are not many elements in a form, we often GET the value of the form element through the GET method. However, if there are many elements in the form, we need to use the POST method to obtain the value of the form element, how can we get the element values of a form? The following is a piece of JS Code to automatically obtain the value of the form element.
Function getFormQueryString (frmID) // frmID is the ID of the form. Please first name an ID in the form {var frmID = document. getElementById (frmID); var I, queryString = "", and = ""; var item; var itemValue; for (I = 0; I <frmID. length; I ++) {item = frmID [I]; if (item. name! = '') {If (item. type = 'select-one') {itemValue = item. options [item. selectedIndex]. value;} else if (item. type = 'checkbox' | item. type = 'Radio ') {if (item. checked = false) {continue;} itemValue = item. value;} else if (item. type = 'button '| item. type = 'submit '| item. type = 'reset' | item. type = 'image') {continue;} else {itemValue = item. value;} itemValue = escape (itemValue); queryString + = and + item. name + '=' + itemValue; and = "&" ;}} return queryString ;}
Call method: Call the preceding JS function in Ajax to obtain the values of all elements in the form.
How does JS obtain the values of form elements under a specified object? See the code!
Document. getElementById ("aaa"). getElemenentsByTagName ("input ")
This is a set of all inputs. If there are no other types (such as type = button), you can.
After js obtains the source code of the current page, it uploads the ajax post value to handle page problems.
In this way, the htmlAction parameter will be passed in the GET method.
In addition, the size of the post value is limited.