zoukankan      html  css  js  c++  java
  • serialize form in javascript

     1  function serialize(form) {
     2         var parts = new Array();
     3         var field = null;
     4 
     5         for (var i = 0, len = form.elements.length; i < len; i++) {
     6             field = form.elements[i];
     7 
     8             switch (field.type) {
     9                 case "select-one":
    10                 case "select-multiple":
    11                     for (var j = 0, optLen = field.options.length; j < optLen; j++) {
    12                         var option = field.options[j];
    13                         if (option.selected) {
    14                             var optValue = "";
    15                             if (option.hasAttribute) {
    16                                 optValue = (option.hasAttribute("value") ? option.value : option.text);
    17                             } else {
    18                                 optValue = (option.attributes["value"].specified ? option.value : option.text);
    19                             }
    20                             parts.push(encodeURIComponent(field.name) + "=" + encodeURIComponent(optValue));
    21                         }
    22                     }
    23                     break;
    24                 case undefined:
    25                 case "file":
    26                 case "submit":
    27                 case "reset":
    28                 case "button":
    29                     break;
    30                 case "radio":
    31                 case "checkbox":
    32                     if (!field.checked) {
    33                         break;
    34                     }
    35                 default:
    36                     parts.push(encodeURIComponent(field.name) + "=" + encodeURIComponent(optValue));
    37             }
    38         }
    39         return parts.join("&");
    40     }
  • 相关阅读:
    动态规划
    Python第二天学习
    Python第一天学习---基础语法
    java易错知识点
    C语言---指针复习
    排序汇总
    课程设计---创建族谱管理系统
    Vue第五篇 Vue的生命周期
    Vue第四篇 Vue路由系统
    Vue第三篇 Vue组件
  • 原文地址:https://www.cnblogs.com/ongoing/p/3082560.html
Copyright © 2011-2022 走看看