zoukankan      html  css  js  c++  java
  • JS JSON & ARRAY 遍历

    下面是拼装json 和 array 的两种情况,这里都是一维的。

    array --

    /*
    * 万能表单提交 针对于项目
    *
    * parm:id 为表单id
     *  */
    
     function go_to_from($id){
        var kv = deal_from($id);
        //解析数据
         for (x in kv)
         {
             alert(x);
             alert(kv[x])
         }
    }
    //万能表单数据处理
    /*
     *   表单查询一般是text search  select 类型
     * */
     function deal_from(id){
            var kv = new Array();
            $("#"+id).find('input').each(function(){
                //构造数组
                $type = $(this).attr('type');
                switch($type)
                {
                    case "search":
                        $v = $(this).val();
                        $k = $(this).attr('name');
                        kv[$k]= $v;
                        break;
                    case "text":
                        $v = $(this).val();
                        $k = $(this).attr('name');
                        kv[$k]= $v;
                        break;
                    case "select":
                        $k = $(this).attr('name');
                        $v = $(this).find('option:selected').val();
                        kv[$k]= $v;
                        break;
                    case "button":
                        break;
                    default:
                        alert("出现了程序未曾预料的情况")
                        return;
                }
            });
            //去掉最后一个逗号
            return kv;
    }

    json --

    *
    * 万能表单提交 针对于项目
    *
    * parm:id 为表单id
     *  */
    
     function go_to_from($id){
        $json = deal_from($id);
        //解析json数据
        $json = $.parseJSON($json);
        for( x in $json){
            alert(x);
            alert($json[x]);
        }
    
    }
    //万能表单数据处理
    /*
     *   表单查询一般是text search  select 类型
     * */
     function deal_from(id){
            $json = "{";
            $("#"+id).find('input').each(function(){
                //构造json
                $type = $(this).attr('type');
                switch($type)
                {
                    case "search":
                        $v = $(this).val();
                        $k = $(this).attr('name');
                        $json += '"'+$k+'":"'+$v+'",';
                        $kv[$k]= $v;
                        break;
                    case "text":
                        $v = $(this).val();
                        $k = $(this).attr('name');
                        $json += '"'+$k+'":"'+$v+'",';break;
                    case "select":
                        $k = $(this).attr('name');
                        $v = $(this).find('option:selected').val();
                        $json += '"'+$k+'":"'+$v+'",';
                        break;
                    case "button":
                        break;
                    default:
                        alert("出现了程序未曾预料的情况")
                        return;
                }
            });
            //去掉最后一个逗号
            $json = $json.substr(0,$json.length-1);
            $json +="}"
            return $json;
    }

    此内容有误;

    积累知识,分享知识,学习知识。
  • 相关阅读:
    VHDL中常用函数类型转换程序包
    Error (10309): VHDL Interface Declaration error in keyboard.vhd(63): interface object "scan_code" of mode out cannot be read. Change object mode to bu
    堆排序
    死锁及如何处理死锁-转载
    红黑书——算法导论
    Randomize select algorithm 随机选择算法
    转载:MATLAB画图常用调整代码
    广告营销学术语
    使用weka进行Cross-validation实验
    设计一个简单的,低耗的能够区分红酒和白酒的感知器(sensor)
  • 原文地址:https://www.cnblogs.com/bin-pureLife/p/4532983.html
Copyright © 2011-2022 走看看