jquery.numberbox.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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){
  14. $(_2).addClass("numberbox-f");
  15. var v=$("<input type=\"hidden\">").insertAfter(_2);
  16. var _3=$(_2).attr("name");
  17. if(_3){
  18. v.attr("name",_3);
  19. $(_2).removeAttr("name").attr("numberboxName",_3);
  20. }
  21. return v;
  22. };
  23. function _4(_5){
  24. var _6=$.data(_5,"numberbox").options;
  25. var fn=_6.onChange;
  26. _6.onChange=function(){
  27. };
  28. _7(_5,_6.parser.call(_5,_6.value));
  29. _6.onChange=fn;
  30. _6.originalValue=_8(_5);
  31. };
  32. function _8(_9){
  33. return $.data(_9,"numberbox").field.val();
  34. };
  35. function _7(_a,_b){
  36. var _c=$.data(_a,"numberbox");
  37. var _d=_c.options;
  38. var _e=_8(_a);
  39. _b=_d.parser.call(_a,_b);
  40. _d.value=_b;
  41. _c.field.val(_b);
  42. $(_a).val(_d.formatter.call(_a,_b));
  43. if(_e!=_b){
  44. _d.onChange.call(_a,_b,_e);
  45. }
  46. };
  47. function _f(_10){
  48. var _11=$.data(_10,"numberbox").options;
  49. $(_10).unbind(".numberbox").bind("keypress.numberbox",function(e){
  50. return _11.filter.call(_10,e);
  51. }).bind("blur.numberbox",function(){
  52. _7(_10,$(this).val());
  53. $(this).val(_11.formatter.call(_10,_8(_10)));
  54. }).bind("focus.numberbox",function(){
  55. var vv=_8(_10);
  56. if(vv!=_11.parser.call(_10,$(this).val())){
  57. $(this).val(_11.formatter.call(_10,vv));
  58. }
  59. });
  60. };
  61. function _12(_13){
  62. if($.fn.validatebox){
  63. var _14=$.data(_13,"numberbox").options;
  64. $(_13).validatebox(_14);
  65. }
  66. };
  67. function _15(_16,_17){
  68. var _18=$.data(_16,"numberbox").options;
  69. if(_17){
  70. _18.disabled=true;
  71. $(_16).attr("disabled",true);
  72. }else{
  73. _18.disabled=false;
  74. $(_16).removeAttr("disabled");
  75. }
  76. };
  77. $.fn.numberbox=function(_19,_1a){
  78. if(typeof _19=="string"){
  79. var _1b=$.fn.numberbox.methods[_19];
  80. if(_1b){
  81. return _1b(this,_1a);
  82. }else{
  83. return this.validatebox(_19,_1a);
  84. }
  85. }
  86. _19=_19||{};
  87. return this.each(function(){
  88. var _1c=$.data(this,"numberbox");
  89. if(_1c){
  90. $.extend(_1c.options,_19);
  91. }else{
  92. _1c=$.data(this,"numberbox",{options:$.extend({},$.fn.numberbox.defaults,$.fn.numberbox.parseOptions(this),_19),field:_1(this)});
  93. $(this).removeAttr("disabled");
  94. $(this).css({imeMode:"disabled"});
  95. }
  96. _15(this,_1c.options.disabled);
  97. _f(this);
  98. _12(this);
  99. _4(this);
  100. });
  101. };
  102. $.fn.numberbox.methods={options:function(jq){
  103. return $.data(jq[0],"numberbox").options;
  104. },destroy:function(jq){
  105. return jq.each(function(){
  106. $.data(this,"numberbox").field.remove();
  107. $(this).validatebox("destroy");
  108. $(this).remove();
  109. });
  110. },disable:function(jq){
  111. return jq.each(function(){
  112. _15(this,true);
  113. });
  114. },enable:function(jq){
  115. return jq.each(function(){
  116. _15(this,false);
  117. });
  118. },fix:function(jq){
  119. return jq.each(function(){
  120. _7(this,$(this).val());
  121. });
  122. },setValue:function(jq,_1d){
  123. return jq.each(function(){
  124. _7(this,_1d);
  125. });
  126. },getValue:function(jq){
  127. return _8(jq[0]);
  128. },clear:function(jq){
  129. return jq.each(function(){
  130. var _1e=$.data(this,"numberbox");
  131. _1e.field.val("");
  132. $(this).val("");
  133. });
  134. },reset:function(jq){
  135. return jq.each(function(){
  136. var _1f=$(this).numberbox("options");
  137. $(this).numberbox("setValue",_1f.originalValue);
  138. });
  139. }};
  140. $.fn.numberbox.parseOptions=function(_20){
  141. var t=$(_20);
  142. return $.extend({},$.fn.validatebox.parseOptions(_20),$.parser.parseOptions(_20,["decimalSeparator","groupSeparator","suffix",{min:"number",max:"number",precision:"number"}]),{prefix:(t.attr("prefix")?t.attr("prefix"):undefined),disabled:(t.attr("disabled")?true:undefined),value:(t.val()||undefined)});
  143. };
  144. $.fn.numberbox.defaults=$.extend({},$.fn.validatebox.defaults,{disabled:false,value:"",min:null,max:null,precision:0,decimalSeparator:".",groupSeparator:"",prefix:"",suffix:"",filter:function(e){
  145. var _21=$(this).numberbox("options");
  146. if(e.which==45){
  147. return ($(this).val().indexOf("-")==-1?true:false);
  148. }
  149. var c=String.fromCharCode(e.which);
  150. if(c==_21.decimalSeparator){
  151. return ($(this).val().indexOf(c)==-1?true:false);
  152. }else{
  153. if(c==_21.groupSeparator){
  154. return true;
  155. }else{
  156. if((e.which>=48&&e.which<=57&&e.ctrlKey==false&&e.shiftKey==false)||e.which==0||e.which==8){
  157. return true;
  158. }else{
  159. if(e.ctrlKey==true&&(e.which==99||e.which==118)){
  160. return true;
  161. }else{
  162. return false;
  163. }
  164. }
  165. }
  166. }
  167. },formatter:function(_22){
  168. if(!_22){
  169. return _22;
  170. }
  171. _22=_22+"";
  172. var _23=$(this).numberbox("options");
  173. var s1=_22,s2="";
  174. var _24=_22.indexOf(".");
  175. if(_24>=0){
  176. s1=_22.substring(0,_24);
  177. s2=_22.substring(_24+1,_22.length);
  178. }
  179. if(_23.groupSeparator){
  180. var p=/(\d+)(\d{3})/;
  181. while(p.test(s1)){
  182. s1=s1.replace(p,"$1"+_23.groupSeparator+"$2");
  183. }
  184. }
  185. if(s2){
  186. return _23.prefix+s1+_23.decimalSeparator+s2+_23.suffix;
  187. }else{
  188. return _23.prefix+s1+_23.suffix;
  189. }
  190. },parser:function(s){
  191. s=s+"";
  192. var _25=$(this).numberbox("options");
  193. if(parseFloat(s)!=s){
  194. if(_25.prefix){
  195. s=$.trim(s.replace(new RegExp("\\"+$.trim(_25.prefix),"g"),""));
  196. }
  197. if(_25.suffix){
  198. s=$.trim(s.replace(new RegExp("\\"+$.trim(_25.suffix),"g"),""));
  199. }
  200. if(_25.groupSeparator){
  201. s=$.trim(s.replace(new RegExp("\\"+_25.groupSeparator,"g"),""));
  202. }
  203. if(_25.decimalSeparator){
  204. s=$.trim(s.replace(new RegExp("\\"+_25.decimalSeparator,"g"),"."));
  205. }
  206. s=s.replace(/\s/g,"");
  207. }
  208. var val=parseFloat(s).toFixed(_25.precision);
  209. if(isNaN(val)){
  210. val="";
  211. }else{
  212. if(typeof (_25.min)=="number"&&val<_25.min){
  213. val=_25.min.toFixed(_25.precision);
  214. }else{
  215. if(typeof (_25.max)=="number"&&val>_25.max){
  216. val=_25.max.toFixed(_25.precision);
  217. }
  218. }
  219. }
  220. return val;
  221. },onChange:function(_26,_27){
  222. }});
  223. })(jQuery);