jquery.form.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /**
  2. * jQuery EasyUI 1.3.4
  3. *
  4. * Copyright (c) 2009-2013 www.jeasyui.com. All rights reserved.
  5. *
  6. * Licensed under the GPL or commercial licenses
  7. * To use it on other terms please contact us: info@jeasyui.com
  8. * http://www.gnu.org/licenses/gpl.txt
  9. * http://www.jeasyui.com/license_commercial.php
  10. *
  11. */
  12. (function($){
  13. function _1(_2,_3){
  14. _3=_3||{};
  15. var _4={};
  16. if(_3.onSubmit){
  17. if(_3.onSubmit.call(_2,_4)==false){
  18. return;
  19. }
  20. }
  21. var _5=$(_2);
  22. if(_3.url){
  23. _5.attr("action",_3.url);
  24. }
  25. var _6="easyui_frame_"+(new Date().getTime());
  26. var _7=$("<iframe id="+_6+" name="+_6+"></iframe>").attr("src",window.ActiveXObject?"javascript:false":"about:blank").css({position:"absolute",top:-1000,left:-1000});
  27. var t=_5.attr("target"),a=_5.attr("action");
  28. _5.attr("target",_6);
  29. var _8=$();
  30. try{
  31. _7.appendTo("body");
  32. _7.bind("load",cb);
  33. for(var n in _4){
  34. var f=$("<input type=\"hidden\" name=\""+n+"\">").val(_4[n]).appendTo(_5);
  35. _8=_8.add(f);
  36. }
  37. _5[0].submit();
  38. }
  39. finally{
  40. _5.attr("action",a);
  41. t?_5.attr("target",t):_5.removeAttr("target");
  42. _8.remove();
  43. }
  44. var _9=10;
  45. function cb(){
  46. _7.unbind();
  47. var _a=$("#"+_6).contents().find("body");
  48. var _b=_a.html();
  49. if(_b==""){
  50. if(--_9){
  51. setTimeout(cb,100);
  52. return;
  53. }
  54. return;
  55. }
  56. var ta=_a.find(">textarea");
  57. if(ta.length){
  58. _b=ta.val();
  59. }else{
  60. var _c=_a.find(">pre");
  61. if(_c.length){
  62. _b=_c.html();
  63. }
  64. }
  65. if(_3.success){
  66. _3.success(_b);
  67. }
  68. setTimeout(function(){
  69. _7.unbind();
  70. _7.remove();
  71. },100);
  72. };
  73. };
  74. function _d(_e,_f){
  75. if(!$.data(_e,"form")){
  76. $.data(_e,"form",{options:$.extend({},$.fn.form.defaults)});
  77. }
  78. var _10=$.data(_e,"form").options;
  79. if(typeof _f=="string"){
  80. var _11={};
  81. if(_10.onBeforeLoad.call(_e,_11)==false){
  82. return;
  83. }
  84. $.ajax({url:_f,data:_11,dataType:"json",success:function(_12){
  85. _13(_12);
  86. },error:function(){
  87. _10.onLoadError.apply(_e,arguments);
  88. }});
  89. }else{
  90. _13(_f);
  91. }
  92. function _13(_14){
  93. var _15=$(_e);
  94. for(var _16 in _14){
  95. var val=_14[_16];
  96. var rr=_17(_16,val);
  97. if(!rr.length){
  98. var f=_15.find("input[numberboxName=\""+_16+"\"]");
  99. if(f.length){
  100. f.numberbox("setValue",val);
  101. }else{
  102. $("input[name=\""+_16+"\"]",_15).val(val);
  103. $("textarea[name=\""+_16+"\"]",_15).val(val);
  104. $("select[name=\""+_16+"\"]",_15).val(val);
  105. }
  106. }
  107. _18(_16,val);
  108. }
  109. _10.onLoadSuccess.call(_e,_14);
  110. _20(_e);
  111. };
  112. function _17(_19,val){
  113. var rr=$(_e).find("input[name=\""+_19+"\"][type=radio], input[name=\""+_19+"\"][type=checkbox]");
  114. rr._propAttr("checked",false);
  115. rr.each(function(){
  116. var f=$(this);
  117. if(f.val()==String(val)||$.inArray(f.val(),val)>=0){
  118. f._propAttr("checked",true);
  119. }
  120. });
  121. return rr;
  122. };
  123. function _18(_1a,val){
  124. var _1b=$(_e);
  125. var cc=["combobox","combotree","combogrid","datetimebox","datebox","combo"];
  126. var c=_1b.find("[comboName=\""+_1a+"\"]");
  127. if(c.length){
  128. for(var i=0;i<cc.length;i++){
  129. var _1c=cc[i];
  130. if(c.hasClass(_1c+"-f")){
  131. if(c[_1c]("options").multiple){
  132. c[_1c]("setValues",val);
  133. }else{
  134. c[_1c]("setValue",val);
  135. }
  136. return;
  137. }
  138. }
  139. }
  140. };
  141. };
  142. function _1d(_1e){
  143. $("input,select,textarea",_1e).each(function(){
  144. var t=this.type,tag=this.tagName.toLowerCase();
  145. if(t=="text"||t=="hidden"||t=="password"||tag=="textarea"){
  146. this.value="";
  147. }else{
  148. if(t=="file"){
  149. var _1f=$(this);
  150. _1f.after(_1f.clone().val(""));
  151. _1f.remove();
  152. }else{
  153. if(t=="checkbox"||t=="radio"){
  154. this.checked=false;
  155. }else{
  156. if(tag=="select"){
  157. this.selectedIndex=-1;
  158. }
  159. }
  160. }
  161. }
  162. });
  163. if($.fn.combo){
  164. $(".combo-f",_1e).combo("clear");
  165. }
  166. if($.fn.combobox){
  167. $(".combobox-f",_1e).combobox("clear");
  168. }
  169. if($.fn.combotree){
  170. $(".combotree-f",_1e).combotree("clear");
  171. }
  172. if($.fn.combogrid){
  173. $(".combogrid-f",_1e).combogrid("clear");
  174. }
  175. _20(_1e);
  176. };
  177. function _21(_22){
  178. _22.reset();
  179. var t=$(_22);
  180. if($.fn.combo){
  181. t.find(".combo-f").combo("reset");
  182. }
  183. if($.fn.combobox){
  184. t.find(".combobox-f").combobox("reset");
  185. }
  186. if($.fn.combotree){
  187. t.find(".combotree-f").combotree("reset");
  188. }
  189. if($.fn.combogrid){
  190. t.find(".combogrid-f").combogrid("reset");
  191. }
  192. if($.fn.datebox){
  193. t.find(".datebox-f").datebox("reset");
  194. }
  195. if($.fn.datetimebox){
  196. t.find(".datetimebox-f").datetimebox("reset");
  197. }
  198. if($.fn.spinner){
  199. t.find(".spinner-f").spinner("reset");
  200. }
  201. if($.fn.timespinner){
  202. t.find(".timespinner-f").timespinner("reset");
  203. }
  204. if($.fn.numberbox){
  205. t.find(".numberbox-f").numberbox("reset");
  206. }
  207. if($.fn.numberspinner){
  208. t.find(".numberspinner-f").numberspinner("reset");
  209. }
  210. _20(_22);
  211. };
  212. function _23(_24){
  213. var _25=$.data(_24,"form").options;
  214. var _26=$(_24);
  215. _26.unbind(".form").bind("submit.form",function(){
  216. setTimeout(function(){
  217. _1(_24,_25);
  218. },0);
  219. return false;
  220. });
  221. };
  222. function _20(_27){
  223. if($.fn.validatebox){
  224. var t=$(_27);
  225. t.find(".validatebox-text:not(:disabled)").validatebox("validate");
  226. var _28=t.find(".validatebox-invalid");
  227. _28.filter(":not(:disabled):first").focus();
  228. return _28.length==0;
  229. }
  230. return true;
  231. };
  232. function _29(_2a,_2b){
  233. $(_2a).find(".validatebox-text:not(:disabled)").validatebox(_2b?"disableValidation":"enableValidation");
  234. };
  235. $.fn.form=function(_2c,_2d){
  236. if(typeof _2c=="string"){
  237. return $.fn.form.methods[_2c](this,_2d);
  238. }
  239. _2c=_2c||{};
  240. return this.each(function(){
  241. if(!$.data(this,"form")){
  242. $.data(this,"form",{options:$.extend({},$.fn.form.defaults,_2c)});
  243. }
  244. _23(this);
  245. });
  246. };
  247. $.fn.form.methods={submit:function(jq,_2e){
  248. return jq.each(function(){
  249. _1(this,$.extend({},$.fn.form.defaults,_2e||{}));
  250. });
  251. },load:function(jq,_2f){
  252. return jq.each(function(){
  253. _d(this,_2f);
  254. });
  255. },clear:function(jq){
  256. return jq.each(function(){
  257. _1d(this);
  258. });
  259. },reset:function(jq){
  260. return jq.each(function(){
  261. _21(this);
  262. });
  263. },validate:function(jq){
  264. return _20(jq[0]);
  265. },disableValidation:function(jq){
  266. return jq.each(function(){
  267. _29(this,true);
  268. });
  269. },enableValidation:function(jq){
  270. return jq.each(function(){
  271. _29(this,false);
  272. });
  273. }};
  274. $.fn.form.defaults={url:null,onSubmit:function(_30){
  275. return $(this).form("validate");
  276. },success:function(_31){
  277. },onBeforeLoad:function(_32){
  278. },onLoadSuccess:function(_33){
  279. },onLoadError:function(){
  280. }};
  281. })(jQuery);