jquery.numberbox.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * EasyUI for jQuery 1.6.10
  3. *
  4. * Copyright (c) 2009-2018 www.jeasyui.com. All rights reserved.
  5. *
  6. * Licensed under the freeware license: http://www.jeasyui.com/license_freeware.php
  7. * To use it on other terms please contact us: info@jeasyui.com
  8. *
  9. */
  10. (function($){
  11. function _1(_2){
  12. var _3=$.data(_2,"numberbox");
  13. var _4=_3.options;
  14. $(_2).addClass("numberbox-f").textbox(_4);
  15. $(_2).textbox("textbox").css({imeMode:"disabled"});
  16. $(_2).attr("numberboxName",$(_2).attr("textboxName"));
  17. _3.numberbox=$(_2).next();
  18. _3.numberbox.addClass("numberbox");
  19. var _5=_4.parser.call(_2,_4.value);
  20. var _6=_4.formatter.call(_2,_5);
  21. $(_2).numberbox("initValue",_5).numberbox("setText",_6);
  22. };
  23. function _7(_8,_9){
  24. var _a=$.data(_8,"numberbox");
  25. var _b=_a.options;
  26. _b.value=parseFloat(_9);
  27. var _9=_b.parser.call(_8,_9);
  28. var _c=_b.formatter.call(_8,_9);
  29. _b.value=_9;
  30. $(_8).textbox("setText",_c).textbox("setValue",_9);
  31. _c=_b.formatter.call(_8,$(_8).textbox("getValue"));
  32. $(_8).textbox("setText",_c);
  33. };
  34. $.fn.numberbox=function(_d,_e){
  35. if(typeof _d=="string"){
  36. var _f=$.fn.numberbox.methods[_d];
  37. if(_f){
  38. return _f(this,_e);
  39. }else{
  40. return this.textbox(_d,_e);
  41. }
  42. }
  43. _d=_d||{};
  44. return this.each(function(){
  45. var _10=$.data(this,"numberbox");
  46. if(_10){
  47. $.extend(_10.options,_d);
  48. }else{
  49. _10=$.data(this,"numberbox",{options:$.extend({},$.fn.numberbox.defaults,$.fn.numberbox.parseOptions(this),_d)});
  50. }
  51. _1(this);
  52. });
  53. };
  54. $.fn.numberbox.methods={options:function(jq){
  55. var _11=jq.data("textbox")?jq.textbox("options"):{};
  56. return $.extend($.data(jq[0],"numberbox").options,{width:_11.width,originalValue:_11.originalValue,disabled:_11.disabled,readonly:_11.readonly});
  57. },cloneFrom:function(jq,_12){
  58. return jq.each(function(){
  59. $(this).textbox("cloneFrom",_12);
  60. $.data(this,"numberbox",{options:$.extend(true,{},$(_12).numberbox("options"))});
  61. $(this).addClass("numberbox-f");
  62. });
  63. },fix:function(jq){
  64. return jq.each(function(){
  65. var _13=$(this).numberbox("options");
  66. _13.value=null;
  67. var _14=_13.parser.call(this,$(this).numberbox("getText"));
  68. $(this).numberbox("setValue",_14);
  69. });
  70. },setValue:function(jq,_15){
  71. return jq.each(function(){
  72. _7(this,_15);
  73. });
  74. },clear:function(jq){
  75. return jq.each(function(){
  76. $(this).textbox("clear");
  77. $(this).numberbox("options").value="";
  78. });
  79. },reset:function(jq){
  80. return jq.each(function(){
  81. $(this).textbox("reset");
  82. $(this).numberbox("setValue",$(this).numberbox("getValue"));
  83. });
  84. }};
  85. $.fn.numberbox.parseOptions=function(_16){
  86. var t=$(_16);
  87. return $.extend({},$.fn.textbox.parseOptions(_16),$.parser.parseOptions(_16,["decimalSeparator","groupSeparator","suffix",{min:"number",max:"number",precision:"number"}]),{prefix:(t.attr("prefix")?t.attr("prefix"):undefined)});
  88. };
  89. $.fn.numberbox.defaults=$.extend({},$.fn.textbox.defaults,{inputEvents:{keypress:function(e){
  90. var _17=e.data.target;
  91. var _18=$(_17).numberbox("options");
  92. return _18.filter.call(_17,e);
  93. },blur:function(e){
  94. $(e.data.target).numberbox("fix");
  95. },keydown:function(e){
  96. if(e.keyCode==13){
  97. $(e.data.target).numberbox("fix");
  98. }
  99. }},min:null,max:null,precision:0,decimalSeparator:".",groupSeparator:"",prefix:"",suffix:"",filter:function(e){
  100. var _19=$(this).numberbox("options");
  101. var s=$(this).numberbox("getText");
  102. if(e.metaKey||e.ctrlKey){
  103. return true;
  104. }
  105. if($.inArray(String(e.which),["46","8","13","0"])>=0){
  106. return true;
  107. }
  108. var tmp=$("<span></span>");
  109. tmp.html(String.fromCharCode(e.which));
  110. var c=tmp.text();
  111. tmp.remove();
  112. if(!c){
  113. return true;
  114. }
  115. if(c=="-"||c==_19.decimalSeparator){
  116. return (s.indexOf(c)==-1)?true:false;
  117. }else{
  118. if(c==_19.groupSeparator){
  119. return true;
  120. }else{
  121. if("0123456789".indexOf(c)>=0){
  122. return true;
  123. }else{
  124. return false;
  125. }
  126. }
  127. }
  128. },formatter:function(_1a){
  129. if(!_1a){
  130. return _1a;
  131. }
  132. _1a=_1a+"";
  133. var _1b=$(this).numberbox("options");
  134. var s1=_1a,s2="";
  135. var _1c=_1a.indexOf(".");
  136. if(_1c>=0){
  137. s1=_1a.substring(0,_1c);
  138. s2=_1a.substring(_1c+1,_1a.length);
  139. }
  140. if(_1b.groupSeparator){
  141. var p=/(\d+)(\d{3})/;
  142. while(p.test(s1)){
  143. s1=s1.replace(p,"$1"+_1b.groupSeparator+"$2");
  144. }
  145. }
  146. if(s2){
  147. return _1b.prefix+s1+_1b.decimalSeparator+s2+_1b.suffix;
  148. }else{
  149. return _1b.prefix+s1+_1b.suffix;
  150. }
  151. },parser:function(s){
  152. s=s+"";
  153. var _1d=$(this).numberbox("options");
  154. if(_1d.prefix){
  155. s=$.trim(s.replace(new RegExp("\\"+$.trim(_1d.prefix),"g"),""));
  156. }
  157. if(_1d.suffix){
  158. s=$.trim(s.replace(new RegExp("\\"+$.trim(_1d.suffix),"g"),""));
  159. }
  160. if(parseFloat(s)!=_1d.value){
  161. if(_1d.groupSeparator){
  162. s=$.trim(s.replace(new RegExp("\\"+_1d.groupSeparator,"g"),""));
  163. }
  164. if(_1d.decimalSeparator){
  165. s=$.trim(s.replace(new RegExp("\\"+_1d.decimalSeparator,"g"),"."));
  166. }
  167. s=s.replace(/\s/g,"");
  168. }
  169. var val=parseFloat(s).toFixed(_1d.precision);
  170. if(isNaN(val)){
  171. val="";
  172. }else{
  173. if(typeof (_1d.min)=="number"&&val<_1d.min){
  174. val=_1d.min.toFixed(_1d.precision);
  175. }else{
  176. if(typeof (_1d.max)=="number"&&val>_1d.max){
  177. val=_1d.max.toFixed(_1d.precision);
  178. }
  179. }
  180. }
  181. return val;
  182. }});
  183. })(jQuery);