zoukankan      html  css  js  c++  java
  • Convert Form Data to json or XML with jQuery

    1. 

    It actually goes to JSON and then to XML, but here's an adaption of json2xml.js specifically for use with jQuery's serializeArray function:

    function json2xml(o, tab) {
      
    var toXml = function(v, name, ind) {
        
    var xml = "";
        
    if (typeof(v) == "object") {
          
    var hasChild = false;
          
    for (var m in v) {
            
    if (m.charAt(0== "@") {
              xml += " " + m.substr(1+ "=\"" + v[m].toString() + "\"";          
            } else {
              hasChild = true;
            }
          }
          
    if (hasChild) {
            
    for (var m in v) {
              
    if (m=="name") {
                xml += "<" + v[m] + ">" + v['value'+ "";
              }
            }
          }
        }
        
    return xml;
      }, xml="";
      
    for (var m in o) {
        xml += toXml(o[m], m, "");
      }

      
    return "
    "+xml+"
    ";

    }

    and then I call it like this: 

    var formjson = $('form#myform').serializeArray();
    //var formxml = $.compactJSON(mjson);
    var formxml = json2xml(formjson);
    alert(formxml);
    $.post("/collect.cgi", { 'data': formxml }, function (data){ });

    http://www.docunext.com/blog/2009/01/convert-form-data-to-xml-with-jquery.html

    2. Serialize form to JSON with jQuery

    $.fn.serializeObject = function()
    {
        
    var o = {};
        
    var a = this.serializeArray();
        $.each(a, function() {
            
    if (o[this.name]) {
                
    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;
    };

    http://stackoverflow.com/questions/1184624/serialize-form-to-json-with-jquery

    3. 

     jquery serializeArray

    $('form').submit(function() {
      alert($(this).serializeArray());
      
    return false;
    });

     http://api.jquery.com/serializeArray/

  • 相关阅读:
    《痕迹识人,面试读心》培训总结之一
    傲游与视频网站广告之战的思考
    EMLS项目推进思考
    二级证丢失如何找回
    Mathematica 讲座
    泊松方程解法
    Windows核心编程-作业
    Win7多用户同时登陆
    C语言文件操作类型速查
    openMP的一点使用经验
  • 原文地址:https://www.cnblogs.com/zyip/p/1837668.html
Copyright © 2011-2022 走看看