Sample Code for Ajax to automatically obtain the value of a form Element Through JS Code, ajaxjs

Source: Internet
Author: User

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.

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.