Home > Web Front-end > JS Tutorial > An example of converting jQuery serialized form values ​​into Json

An example of converting jQuery serialized form values ​​into Json

小云云
Release: 2018-01-06 13:23:34
Original
1584 people have browsed it

This article mainly introduces the relevant information about converting Jquery serialized form values ​​into Json. It is very good and has reference value. Friends who need it can refer to it. I hope it can help everyone.

The serialized form value string can be obtained through $("#form").serialize().

For example:


a=1&b=2&c=3&d=4&e=5
Copy after login

Serialize the form value in the form of an array through $("#form").serializeArray()Output .


[ 
 {name: 'firstname', value: 'Hello'}, 
 {name: 'lastname', value: 'World'},
 {name: 'alias'}, // 值为空
]
Copy after login

None of them satisfy the children’s wish to get Json. After stack overflow, I found such a method


$.fn.serializeObject = function()
{
 var o = {};
 var a = this.serializeArray();
 $.each(a, function() {
 if (o[this.name] !== undefined) {
  if (!o[this.name].push) {
  o[this.name] = [o[this.name]];
  }
  o[this.name].push(this.value || '');
 } else {
  o[this.name] = this.value || '';
 }
 });
 return o;
};
Copy after login

Then you can get Json through $("#form").serializeObject(); Content.

Related recommendations:

PHP uses forms to access single and multiple form values_PHP tutorial

php passes two form values Question, please give me some advice

Detailed explanation of python recursive query menu and convert it into json example code

The above is the detailed content of An example of converting jQuery serialized form values ​​into Json. 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