search
HomeWeb Front-endJS TutorialDetailed explanation of form serialization (graphic tutorial)
Detailed explanation of form serialization (graphic tutorial)May 19, 2018 am 09:41 AM
formGraphics and textSerialization

This article mainly introduces the form serialization operation. By serializing the key values ​​​​in the form into a submittable string, and attaching a code example to explain the results in detail after operation, friends in need can refer to the following

Serialization of the form, that is, serializing the key values ​​​​in the form into a submittable string

Form

 <form id="target">
  <select name="age">
   <option value="age1">20</option>
   <option value="age2" selected>21</option>
  </select>
  <input name="name" value="Cynthia">
  <label>passsword</label>
  <input type="password" name="password" value="123456">
  <input type="hidden" name="salery" value="3333">
  <textarea name="description" cols="15" rows="5">description</textarea>
  <input type="checkbox" name="hobby" value="football" checked> Football
  <input type="checkbox" name="hobby" value="basketball"> Basketball
  <input type="radio" name="sex" value="Female" checked> Female
  <input type="radio" name="sex" value="Male"> Male
 </form>


##Method 1

function serializeForm1(form){ 
   var setForm = ""; 
   for(var key in form){ 
    if(form.hasOwnProperty(key)){ 
     setForm += &#39;"&#39;+form[key].name+&#39;"&#39;+&#39;:&#39;+&#39;"&#39;+form[key].value + &#39;"&#39;+&#39;,&#39;;
    }
   }
   setForm = "{" + setForm.slice(0,setForm.length -1) + "}";
   console.log(setForm);
   // console.log(JSON.parse(setForm));
   return JSON.parse(setForm);
  }
   
  // 调用
  var oForm = document.getElementById(&#39;target&#39;);
  console.log(serializeForm3(oForm));

Result:

Method 2

 function serializeForm2(form) {
   var parts = [];
   for (var i = 0, i1 = form.elements.length; i < i1; i++) {
    var field = form.elements[i];
    switch (field.type) {
     case &#39;select-one&#39;:
     case &#39;select-multiple&#39;:
      if (field.type.length) {
       for (var j = 0, j1 = field.options.length; j < j1; j++) {
        var option = field.options[j];
        if (option.selected) {
         var optionValue = &#39;&#39;;
         if (option.hasAttribute(&#39;value&#39;) && option.attributes[&#39;value&#39;].specified) {
          //specified表明是否有此属性,有的话返回true,若定义了此属性但尚未添加到元素中也返回true。
          optionValue = option.value;
         } else {
          optionValue = optionValue.text;
         }
         parts.push(encodeURIComponent(field.name) + &#39;=&#39; + encodeURIComponent(optionValue));
        }
       }
      }
      break;
     case undefined:
     case &#39;file&#39;:
     case &#39;submit&#39;:
     case &#39;reset&#39;:
     case &#39;button&#39;:
      break;
     case &#39;radio&#39;:
     case &#39;checkbox&#39;:
      if(!field.checked){
       break;
      }else{
       parts.push(encodeURIComponent(field.name) + &#39;=&#39; + encodeURIComponent(field.dataset[&#39;index&#39;]));
       break;
      }
     default:
      if(field.name.length){
       parts.push(encodeURIComponent(field.name) + &#39;=&#39; + encodeURIComponent(field.value));
      }
    }
   }
   return parts.join(&#39;&&#39;);
  }
   
  // 调用
  var oForm = document.getElementById(&#39;target&#39;);
    console.log(serializeForm2(oForm));

Result:

Method 3

 function serializeForm3(form){
   if(!form||form.tagName.toUpperCase()!=&#39;FORM&#39;){
    return false;
   }
   var res=[];
   var tag,tagType,tagVal,tagName;
   for(var i=0;i<form.length;i++){
    tag=form[i];
    tagType=form[i].type;
    tagVal=form[i].value;
    tagName=form[i].name; 
    var reg1=/[&#39;textarea&#39;|&#39;text&#39;|&#39;passsword&#39;|&#39;label&#39;]/g;
    var reg2=/[&#39;radio&#39;|&#39;checkbox&#39;]/g;
    var reg3=/[&#39;select&#39;]/g;
    if(reg1.test(tagType)){
     res.push(encodeURIComponent(tagName)+"="+encodeURIComponent(tagVal));
    }else if(reg2.test(tagType)&&tag.checked){
     res.push(encodeURIComponent(tagName)+"="+encodeURIComponent(tagVal)); 
    }else if(reg3.test(tagType)){
     for(var j=0;j<tag.options.length;j++){
      if(tag.options[j].selected){
      res.push(encodeURIComponent(tagVal)+"="+encodeURIComponent(tag.options[j].value||tag.options[j].text)); 
      }
     }
    }else{}
   } 
   return res.join(" & ");
  }
   
  // 调用
  var oForm = document.getElementById(&#39;target&#39;);
  console.log(serializeForm3(oForm));

Result:

The above is the entire content of this article, I hope it will be helpful to everyone~

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

nodejs Detailed explanation of express implementation file upload case

webpack.config .jsParameter usage case

p5.jsHow to load images

The above is the detailed content of Detailed explanation of form serialization (graphic tutorial). For more information, please follow other related articles on the PHP Chinese website!

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
在Java中,我们如何使用flexjson序列化对象列表?在Java中,我们如何使用flexjson序列化对象列表?Sep 05, 2023 pm 11:09 PM

Flexjson是一个轻量级库,用于序列化和反序列化Java对象>和来自JSON格式。我们可以使用JSONSerializer类的serialize()方法序列化对象列表。此方法可以对目标实例执行浅层序列化。我们需要将列表类型的对象列表作为参数传递给serialize()方法。语法publicStringserialize(Objecttarget)示例importflexjson.JSONSerializer;importjava.util.*;publicclassJsonSerial

Java序列化如何影响性能?Java序列化如何影响性能?Apr 16, 2024 pm 06:36 PM

序列化对Java性能的影响:序列化过程依赖于反射,会显著影响性能。序列化需要创建字节流存储对象数据,导致内存分配和处理成本。序列化大对象会消耗大量内存和时间。序列化后的对象在网络上传输时会增加负载量。

C++ 函数库如何进行序列化和反序列化?C++ 函数库如何进行序列化和反序列化?Apr 18, 2024 am 10:06 AM

C++函数库序列化和反序列化指南序列化:创建输出流并将其转换为存档格式。将对象序列化到存档中。反序列化:创建输入流并将其从存档格式恢复。从存档中反序列化对象。实战示例:序列化:创建输出流。创建存档对象。创建对象并将其序列化到存档中。反序列化:创建输入流。创建存档对象。创建对象并从存档中反序列化。

PHP数据处理技巧:如何使用serialize和unserialize函数实现数据序列化与反序列化PHP数据处理技巧:如何使用serialize和unserialize函数实现数据序列化与反序列化Jul 29, 2023 am 10:49 AM

PHP数据处理技巧:如何使用serialize和unserialize函数实现数据序列化与反序列化序列化和反序列化是在计算机科学中常用的数据处理技巧之一。在PHP中,我们可以使用serialize()和unserialize()函数来实现数据的序列化和反序列化操作。本文将为您详细介绍如何使用这两个函数,并提供相关代码示例。一、什么是序列化和反序列化在计算机编

如何使用Java中的Jackson库对属性的顺序进行序列化?如何使用Java中的Jackson库对属性的顺序进行序列化?Aug 28, 2023 pm 12:45 PM

@JsonPropertyOrder是在类级别使用的注释。它采用字段列表作为属性,该列表定义字段在对象JSON序列化生成的字符串中出现的顺序。可以首先序列化注释声明中包含的属性(按定义的顺序),然​​后序列化定义中未包含的任何属性。语法public@interfaceJsonPropertyOrder示例importcom.fasterxml.jackson.core.*;importcom.fasterxml.jackson.databind.*;importcom.fasterxml.jac

如何使用Java中的flexjson库序列化一个map?如何使用Java中的flexjson库序列化一个map?Aug 26, 2023 pm 08:13 PM

Flexjson是一个轻量级库,用于将Java对象序列化为JSON格式以及反序列化为JSON格式。我们还可以使用JSONSerializer类的serialize()方法来序列化Map,它对目标实例执行浅层序列化。语法publicStringserialize(Objecttarget)示例importflexjson.JSONSerializer;importjava.util.*;publicclassJsonSerializeMapTest{&nbsp;&nbsp;publ

在Java中,序列化和反序列化有什么区别?在Java中,序列化和反序列化有什么区别?Apr 16, 2024 am 08:54 AM

序列化将对象转换为字节序列,反序列化将字节序列还原为对象。序列化用于持久化或传输对象,而反序列化用于重建对象。实战案例中,用户对象序列化写入文件,然后反序列化读出,演示了序列化和反序列化在Java中的实际应用。

PHP数组的序列化和反序列化方法和注意事项PHP数组的序列化和反序列化方法和注意事项Jul 16, 2023 pm 06:37 PM

PHP数组的序列化和反序列化方法和注意事项在PHP中,数组是一种非常常见和重要的数据类型。当我们需要在不同的程序之间或在不同的请求之间传递数组时,就需要将数组进行序列化和反序列化。本文将介绍PHP中数组的序列化和反序列化方法以及相关的注意事项。序列化数组在PHP中,可以使用serialize()函数将一个数组序列化为一个字符串。该函数的用法如下所示:$arr

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)