Maison  >  Article  >  interface Web  >  jQuery基于json对象自动给表单元素赋值

jQuery基于json对象自动给表单元素赋值

巴扎黑
巴扎黑original
2016-11-25 15:27:431869parcourir

为了提高前端小伙伴的开发效率,造了个基于json对象根据表单元素的name属性自动赋值的轮子 

 json2form:function(obj){
var nodeParent = null;
var value = undefined;
var $el = null;
var nodeName = "";
    for(var i in obj){
     value= obj[i] ;
           if(value === undefined || value===null){
               continue;
           }
       if(typeof value == 'object'){
           nodeParent=obj.nodeParent;
           value.nodeParent=nodeParent?nodeParent+"."+i : i;
if(value instanceof Array){
for(var mm=0;mm<value.length;mm++){
var ms=value[mm];
if(typeof ms == &#39;object&#39;){
nodeParent=ms.nodeParent;
ms.nodeParent=ms.nodeParent?ms.nodeParent+"."+i+"["+mm+"]":i+"["+mm+"]";
arguments.callee(ms);
}
}
$el=$("[name=&#39;"+i+"&#39;]");
if($el.is(":checkbox")){
$el.each(function(){
if($(this).val() == value){
$(this).prop("checked",true);
}
})
}
else if($el.is(":radio")){
$el.each(function(){
if($(this).val() == value){
$(this).prop("checked",true);
}
})
}
}else{  //递归
arguments.callee(value);
}
}
       else{
           nodeName=obj.nodeParent?obj.nodeParent+"."+i : i ;
           $el=$("[name=&#39;"+nodeName+"&#39;]");
           if($el.length > 0){
           // console.log("匹配数据名称:"+nodeName+"值:"+value);
               if($el.is(":text")||$el.attr("type")=="hidden"){
               if($el.data("money") && $el.data("money") == "money"){
               value = outputdollars(value);
               }
                   $el.val(value);
                     
              }else if($el.is(":radio")){
                  $el.each(function(){
                     if($(this).val()==value){
                         $(this).prop("checked",true);
                     }
                  })
              }
              else if($el.is("select")){
                 $el.find("option").filter(function(){return $(this).val() == obj[i];}).prop("selected",true);
              }else if($el.is("textarea")){
                 $el.val(value)
              }
           }
       }
   } 
 
}

注意: 表单的name属于与json对象的属性名为一致,保持继承链。例如 input name='a.b.c'   表示json对象里面的a属性里面的b属性的c属性。

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn