针对form表单赋值封装

  1 (function ($){
  2  $.fn.extend({
  3       exajax:function(url,opts,convert){
  4       var ajaxParam = {
  5           url:url,
  6           async : false,    // 同步
  7           type:'POST', // 请求类型
  8           cache:false, // 不缓存
  9           dataType:'json',
 10           success:'',
 11           error : ''
 12 };
 13 opts =opts||{};
 14 for(var k in opts){
 15     if(ajaxParam.hasOwnProperty(k)||'data'==k){
 16           if(k=='data'){
 17              try{
 18              var params = opts['data'];
 19      if(!convert){
 20        params = JSON.stringify(params);
 21        ajaxParam.contentType= 'application/json'; 
 22     }
 23        ajaxParam.data= params;
 24 }catch(e){    
 25 }
 26 }else
 27 if(k=='success'){
 28 var success = opts[k];
 29 ajaxParam[k] =function(data){
 30 if(success){
 31 if(typeof success == 'function'){
 32 success.apply(success,[data]);
 33 }else{
 34 if(success.message){
 35 BootstrapDialog.show({
 36 title: success.title||'系统消息',
 37 message: success.message||'操作成功',
 38 buttons: [{
 39 label: '确定',
 40 action: function(dialogItself){
 41 dialogItself.close();
 42 }
 43 }]
 44 });
 45 }    
 46 
 47 }
 48 }
 49 }
 50 }else 
 51 if(k=='error'){
 52 var error = opts[k];
 53 ajaxParam[k] =function(e) {
 54 if(error){
 55 if(typeof error == 'function'){
 56 error.apply(error,[e]);    
 57 }else{
 58 if(error.message){
 59 BootstrapDialog.show({
 60 title: error.title||'系统错误',
 61 message: error.message||'系统未知错误 - '+e.description,
 62 buttons: [{
 63 label: '确定',
 64 action: function(dialogItself){
 65 dialogItself.close();
 66 }
 67 }]
 68 });
 69 }    
 70 }
 71 }
 72 }
 73 }else{
 74 ajaxParam[k] = opts[k];    
 75 }
 76 }
 77 }
 78 $.ajax(ajaxParam);
 79 },
 80 getJsonFormValues:function(){
 81 el = $(this);
 82 var elementsObj = el.get(0).elements;
 83 var ret = {};    
 84 if(elementsObj){
 85 $.each(elementsObj,function(index, obj){
 86 val = null;
 87 if (obj.tagName == "INPUT" || obj.tagName == "SELECT") {
 88 val = obj.value;
 89 if(obj.name == "id" && obj.value == ""){
 90 val = null;
 91 }
 92 if(obj.type == 'checkbox' || obj.type == 'radio'){
 93 if($(obj).is(':checked')){
 94 var result;
 95 if(typeof obj.value !=undefined){
 96 result = obj.value;
 97 }
 98 if(obj.type == 'radio'){
 99 ret[obj.name]=result;
100 }else{
101 if(!ret[obj.name]){
102 ret[obj.name] = [];
103 }
104 ret[obj.name].push(result);
105 }
106 }
107 }
108 if(obj.type != 'checkbox' && obj.type != 'radio'){
109 ret[obj.name] = val;
110 }
111 }
112 });
113 }
114 return ret;
115 },
116 getFormValues:function(){
117 el = $(this);
118 var result = $(this).serialize();
119 //var elementsObj = el.get(0).elements;
120 //var ret = {};
121 //if(elementsObj){
122 //$.each(elementsObj,function(index, obj){
123 //if(obj.type == 'hidden'){
124 //result += "&" +obj.name+"="+obj.value;
125 //}
126 //});
127 //}
128 return result;
129 },
130 getFormValueNoHidden:function(){
131 el = $(this);
132 var result = $(this).serialize();
133 var elementsObj = el.get(0).elements;
134 var ret = {};    
135 if(elementsObj){
136 $.each(elementsObj,function(index, obj){
137 });
138 }
139 return result;
140 },
141 setFormValues:function(data){
142 el = $(this);
143 var elementsObj = el.get(0).elements;
144 if (elementsObj) {
145 $.each(elementsObj,function(index, obj){
146 if (obj.tagName == "INPUT" || obj.tagName == "SELECT") {
147 val = null;
148 if(data[obj.name] != undefined){
149 var func = data[obj.name];
150 if(typeof(func)=='function'){
151 val = func.apply(func,[val,obj,elementsObj]);
152 }else{
153 val = func;
154 }
155 }
156 //if(format&&format[obj.name]!= undefined){
157 //func = format[obj.name];
158 //if(typeof(func)=='function'){
159 //val = func.apply(func,[val,obj,data]);
160 //}else{
161 //val = func;
162 //}
163 //}
164 if(obj.type == 'checkbox' || obj.type == 'radio'){
165 if(obj.value !=undefined && val instanceof Array){
166 for(var v in val){
167 if(val[v]==obj.value){
168 $(obj).prop("checked",true);
169 }
170 }
171 }else{
172 if(null!=val &&val!=undefined&&(val == true || val == 1||val==obj.value)){
173 $(obj).prop("checked",true);
174 }else{
175 $(obj).prop("checked",false);
176 }
177 }
178 }else if(obj.type=='file'){
179 // do nothing;
180 }else{
181 obj.value = val;
182 }
183 }
184 });    
185 }
186 }
187 });
188 })(jQuery);
原文地址:https://www.cnblogs.com/benmumu/p/11097772.html