jquery.numberbox.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * jQuery EasyUI 1.5.2
  3. *
  4. * Copyright (c) 2009-2017 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. },fix:function(jq){
  58. return jq.each(function(){
  59. var _12=$(this).numberbox("options");
  60. _12.value=null;
  61. var _13=_12.parser.call(this,$(this).numberbox("getText"));
  62. $(this).numberbox("setValue",_13);
  63. });
  64. },setValue:function(jq,_14){
  65. return jq.each(function(){
  66. _7(this,_14);
  67. });
  68. },clear:function(jq){
  69. return jq.each(function(){
  70. $(this).textbox("clear");
  71. $(this).numberbox("options").value="";
  72. });
  73. },reset:function(jq){
  74. return jq.each(function(){
  75. $(this).textbox("reset");
  76. $(this).numberbox("setValue",$(this).numberbox("getValue"));
  77. });
  78. }};
  79. $.fn.numberbox.parseOptions=function(_15){
  80. var t=$(_15);
  81. return $.extend({},$.fn.textbox.parseOptions(_15),$.parser.parseOptions(_15,["decimalSeparator","groupSeparator","suffix",{min:"number",max:"number",precision:"number"}]),{prefix:(t.attr("prefix")?t.attr("prefix"):undefined)});
  82. };
  83. $.fn.numberbox.defaults=$.extend({},$.fn.textbox.defaults,{inputEvents:{keypress:function(e){
  84. var _16=e.data.target;
  85. var _17=$(_16).numberbox("options");
  86. return _17.filter.call(_16,e);
  87. },blur:function(e){
  88. $(e.data.target).numberbox("fix");
  89. },keydown:function(e){
  90. if(e.keyCode==13){
  91. $(e.data.target).numberbox("fix");
  92. }
  93. }},min:null,max:null,precision:0,decimalSeparator:".",groupSeparator:"",prefix:"",suffix:"",filter:function(e){
  94. var _18=$(this).numberbox("options");
  95. var s=$(this).numberbox("getText");
  96. if(e.metaKey||e.ctrlKey){
  97. return true;
  98. }
  99. if($.inArray(String(e.which),["46","8","13","0"])>=0){
  100. return true;
  101. }
  102. var tmp=$("<span></span>");
  103. tmp.html(String.fromCharCode(e.which));
  104. var c=tmp.text();
  105. tmp.remove();
  106. if(!c){
  107. return true;
  108. }
  109. if(c=="-"||c==_18.decimalSeparator){
  110. return (s.indexOf(c)==-1)?true:false;
  111. }else{
  112. if(c==_18.groupSeparator){
  113. return true;
  114. }else{
  115. if("0123456789".indexOf(c)>=0){
  116. return true;
  117. }else{
  118. return false;
  119. }
  120. }
  121. }
  122. },formatter:function(_19){
  123. if(!_19){
  124. return _19;
  125. }
  126. _19=_19+"";
  127. var _1a=$(this).numberbox("options");
  128. var s1=_19,s2="";
  129. var _1b=_19.indexOf(".");
  130. if(_1b>=0){
  131. s1=_19.substring(0,_1b);
  132. s2=_19.substring(_1b+1,_19.length);
  133. }
  134. if(_1a.groupSeparator){
  135. var p=/(\d+)(\d{3})/;
  136. while(p.test(s1)){
  137. s1=s1.replace(p,"$1"+_1a.groupSeparator+"$2");
  138. }
  139. }
  140. if(s2){
  141. return _1a.prefix+s1+_1a.decimalSeparator+s2+_1a.suffix;
  142. }else{
  143. return _1a.prefix+s1+_1a.suffix;
  144. }
  145. },parser:function(s){
  146. s=s+"";
  147. var _1c=$(this).numberbox("options");
  148. if(_1c.prefix){
  149. s=$.trim(s.replace(new RegExp("\\"+$.trim(_1c.prefix),"g"),""));
  150. }
  151. if(_1c.suffix){
  152. s=$.trim(s.replace(new RegExp("\\"+$.trim(_1c.suffix),"g"),""));
  153. }
  154. if(parseFloat(s)!=_1c.value){
  155. if(_1c.groupSeparator){
  156. s=$.trim(s.replace(new RegExp("\\"+_1c.groupSeparator,"g"),""));
  157. }
  158. if(_1c.decimalSeparator){
  159. s=$.trim(s.replace(new RegExp("\\"+_1c.decimalSeparator,"g"),"."));
  160. }
  161. s=s.replace(/\s/g,"");
  162. }
  163. var val=parseFloat(s).toFixed(_1c.precision);
  164. if(isNaN(val)){
  165. val="";
  166. }else{
  167. if(typeof (_1c.min)=="number"&&val<_1c.min){
  168. val=_1c.min.toFixed(_1c.precision);
  169. }else{
  170. if(typeof (_1c.max)=="number"&&val>_1c.max){
  171. val=_1c.max.toFixed(_1c.precision);
  172. }
  173. }
  174. }
  175. return val;
  176. }});
  177. })(jQuery);