码迷,mamicode.com
首页 > Web开发 > 详细

jquery 获取表单json ,jquery为表单赋值json扩展

时间:2017-10-02 16:43:40      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:json   cat   tostring   for   use   query   使用   check   turn   

jquery 获取表单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; } });

 

赋值使用:

function load(){
                
                var qId = $.getUrlParam(window.location.href,‘id‘);
                
                var data = {id :qId};
                var url = "/api/sysuser/info";
                $.get(url,data,function(result){
                    if(result.status=="1")
                      {
                        //var template= $(‘#form1‘).tmpl(result.data);
                        //$(‘#form1‘).replaceWith(template);
                        $.setForm(‘#form1‘,result.data);
                        
                      }
                },‘json‘);
                
            }

 

jquery 获取表单json ,jquery为表单赋值json扩展

标签:json   cat   tostring   for   use   query   使用   check   turn   

原文地址:http://www.cnblogs.com/wenming205/p/7620265.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!