Home  >  Article  >  Web Front-end  >  Function used with jquery serializeArray(), mainly to facilitate form submission_jquery

Function used with jquery serializeArray(), mainly to facilitate form submission_jquery

WBOY
WBOYOriginal
2016-05-16 18:11:041121browse

.serializeArray() serializes table elements (similar to '.serialize()' method) and returns JSON data structure data. (Taken from jquery documentation).
There is the following form window, code:

Copy code The code is as follows:

< ;form action="" method="post" id="tf">









Name:

Contact mobile phone:






JavaScript code to process the form:
Copy code The code is as follows:

<script> <br>$(function () { <br>$("#butsubmit") .click(function(){ <br>var data = convertArray($("#tf").serializeArray()); <br>$.post(url, data, function (d) {},"json") ; <br>}); <br>}) <br>function convertArray(o) { //This function is mainly recommended. It converts jquery serialized values ​​into name:value form. <br>var v = {}; <br>for (var i in o) { <br>if (typeof (v[o[i].name]) == 'undefined') v[o[i]. name] = o[i].value; <br>else v[o[i].name] = "," o[i].value; <br>} <br>return v; <br>} <br></script>
Statement:
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