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!='' ) { 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; }
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!