zoukankan      html  css  js  c++  java
  • 用Json给表单赋值

    $.extend({
        setForm :function(frm,jsonValue) {   
        var obj=$(frm);  
        $.each(jsonValue, function (name, ival) {  
            var $oinput = obj.find("input[name=" + name + "]");   
            if ($oinput.attr("type")== "radio" || $oinput.attr("type")== "checkbox"){  
                 $oinput.each(function(){  
                     if(Object.prototype.toString.apply(ival) == '[object Array]'){// 是复选框,并且是数组
                          for(var i=0;i<ival.length;i++){  
                              if($(this).val()==ival[i])  
                                 $(this).attr("checked", "checked");  
                          }  
                     }else{  
                         if($(this).val()==ival)  
                            $(this).attr("checked", "checked");  
                     }  
                 });  
            }else if($oinput.attr("type")== "textarea"){// 多行文本框
                obj.find("[name="+name+"]").html(ival);  
            }else{  
                 obj.find("[name="+name+"]").val(ival);   
            }  
        });
    },
    getFormJson:function(frm) {
            var o = {};
            var a = $(frm).serializeArray();
    
            $.each(a, function() {
                if (this.name == "password") {
                    //this.value = $.md5(this.value) //md5操作 
              this.value=this.value;
                }
                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;
    }
    });

    以上是扩展

    下面是调用方式:

       $.setForm('#form1',jsonData);

    以上内容转载自:https://www.cnblogs.com/cutone/p/7126094.html

  • 相关阅读:
    Vmware虚拟机的单用户模式
    Xshell密钥认证
    PuTTY密钥认证
    CentOS 7在NAT模式下配置静态IP
    使用OllyDbg破解EasyCrackMe
    2017 计蒜之道 初赛 第五场 A. UCloud 机房的网络搭建
    2017 计蒜之道 初赛 第五场 A. UCloud 机房的网络搭建
    Kafka常用命令
    选择器和层叠
    语义化
  • 原文地址:https://www.cnblogs.com/LearningC/p/8118639.html
Copyright © 2011-2022 走看看