Home > Web Front-end > JS Tutorial > body text

Automatically obtain form element values ​​through JS code in Ajax

不言
Release: 2018-07-02 16:08:34
Original
1514 people have browsed it

This article mainly introduces the automatic acquisition of form element values ​​through JS code in Ajax. It has certain reference value. Now I share it with you. Friends in need can refer to it.

If there are not many form elements We often use the GET method to obtain the form element value, but if there are many form elements, we need to use the POST method to obtain the form element value. So how to obtain the form element value?

We are here When using Ajax, you usually need to obtain the form element value and then send it to the background server-side program for processing. If there are not many form elements, we often use the GET method to obtain the form element values. But if there are many form elements, we need to use the POST method to obtain the form element values. So how to obtain the form element values? The following is a piece of JS code that can automatically obtain the value of the form element.

function getFormQueryString(frmID) //frmID是表单的ID号,请在表单form中先命名一个ID号
{
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!=&#39;&#39; )
{
if ( item.type == &#39;select-one&#39; )
{
itemValue = item.options[item.selectedIndex].value;
}
else if ( item.type==&#39;checkbox&#39; || item.type==&#39;radio&#39;)
{
if ( item.checked == false )
{
continue; 
}
itemValue = item.value;
}
else if ( item.type == &#39;button&#39; || item.type == &#39;submit&#39; || item.type == &#39;reset&#39; || item.type == &#39;image&#39;)
{
continue;
}
else
{
itemValue = item.value;
}
itemValue = escape(itemValue);
queryString += and + item.name + &#39;=&#39; + itemValue;
and="&";
}
}
return queryString;
}
Copy after login

Calling method: Call the above JS function directly in Ajax to get the values ​​of all elements in the form.

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Ajax bootstrap beautifies web pages and implements page loading, deletion and viewing details code

About AJax and Issues with Jsonp cross-domain access

The above is the detailed content of Automatically obtain form element values ​​through JS code in Ajax. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!