jquery.numberbox.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * jQuery EasyUI 1.4.4
  3. *
  4. * Copyright (c) 2009-2015 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. var _9=_b.parser.call(_8,_9);
  27. var _c=_b.formatter.call(_8,_9);
  28. _b.value=_9;
  29. $(_8).textbox("setText",_c).textbox("setValue",_9);
  30. _c=_b.formatter.call(_8,$(_8).textbox("getValue"));
  31. $(_8).textbox("setText",_c);
  32. };
  33. $.fn.numberbox=function(_d,_e){
  34. if(typeof _d=="string"){
  35. var _f=$.fn.numberbox.methods[_d];
  36. if(_f){
  37. return _f(this,_e);
  38. }else{
  39. return this.textbox(_d,_e);
  40. }
  41. }
  42. _d=_d||{};
  43. return this.each(function(){
  44. var _10=$.data(this,"numberbox");
  45. if(_10){
  46. $.extend(_10.options,_d);
  47. }else{
  48. _10=$.data(this,"numberbox",{options:$.extend({},$.fn.numberbox.defaults,$.fn.numberbox.parseOptions(this),_d)});
  49. }
  50. _1(this);
  51. });
  52. };
  53. $.fn.numberbox.methods={options:function(jq){
  54. var _11=jq.data("textbox")?jq.textbox("options"):{};
  55. return $.extend($.data(jq[0],"numberbox").options,{width:_11.width,originalValue:_11.originalValue,disabled:_11.disabled,readonly:_11.readonly});
  56. },fix:function(jq){
  57. return jq.each(function(){
  58. $(this).numberbox("setValue",$(this).numberbox("getText"));
  59. });
  60. },setValue:function(jq,_12){
  61. return jq.each(function(){
  62. _7(this,_12);
  63. });
  64. },clear:function(jq){
  65. return jq.each(function(){
  66. $(this).textbox("clear");
  67. $(this).numberbox("options").value="";
  68. });
  69. },reset:function(jq){
  70. return jq.each(function(){
  71. $(this).textbox("reset");
  72. $(this).numberbox("setValue",$(this).numberbox("getValue"));
  73. });
  74. }};
  75. $.fn.numberbox.parseOptions=function(_13){
  76. var t=$(_13);
  77. return $.extend({},$.fn.textbox.parseOptions(_13),$.parser.parseOptions(_13,["decimalSeparator","groupSeparator","suffix",{min:"number",max:"number",precision:"number"}]),{prefix:(t.attr("prefix")?t.attr("prefix"):undefined)});
  78. };
  79. $.fn.numberbox.defaults=$.extend({},$.fn.textbox.defaults,{inputEvents:{keypress:function(e){
  80. var _14=e.data.target;
  81. var _15=$(_14).numberbox("options");
  82. return _15.filter.call(_14,e);
  83. },blur:function(e){
  84. var _16=e.data.target;
  85. $(_16).numberbox("setValue",$(_16).numberbox("getText"));
  86. },keydown:function(e){
  87. if(e.keyCode==13){
  88. var _17=e.data.target;
  89. $(_17).numberbox("setValue",$(_17).numberbox("getText"));
  90. }
  91. }},min:null,max:null,precision:0,decimalSeparator:".",groupSeparator:"",prefix:"",suffix:"",filter:function(e){
  92. var _18=$(this).numberbox("options");
  93. var s=$(this).numberbox("getText");
  94. if(e.which==13){
  95. return true;
  96. }
  97. if(e.which==45){
  98. return (s.indexOf("-")==-1?true:false);
  99. }
  100. var c=String.fromCharCode(e.which);
  101. if(c==_18.decimalSeparator){
  102. return (s.indexOf(c)==-1?true:false);
  103. }else{
  104. if(c==_18.groupSeparator){
  105. return true;
  106. }else{
  107. if((e.which>=48&&e.which<=57&&e.ctrlKey==false&&e.shiftKey==false)||e.which==0||e.which==8){
  108. return true;
  109. }else{
  110. if(e.ctrlKey==true&&(e.which==99||e.which==118)){
  111. return true;
  112. }else{
  113. return false;
  114. }
  115. }
  116. }
  117. }
  118. },formatter:function(_19){
  119. if(!_19){
  120. return _19;
  121. }
  122. _19=_19+"";
  123. var _1a=$(this).numberbox("options");
  124. var s1=_19,s2="";
  125. var _1b=_19.indexOf(".");
  126. if(_1b>=0){
  127. s1=_19.substring(0,_1b);
  128. s2=_19.substring(_1b+1,_19.length);
  129. }
  130. if(_1a.groupSeparator){
  131. var p=/(\d+)(\d{3})/;
  132. while(p.test(s1)){
  133. s1=s1.replace(p,"$1"+_1a.groupSeparator+"$2");
  134. }
  135. }
  136. if(s2){
  137. return _1a.prefix+s1+_1a.decimalSeparator+s2+_1a.suffix;
  138. }else{
  139. return _1a.prefix+s1+_1a.suffix;
  140. }
  141. },parser:function(s){
  142. s=s+"";
  143. var _1c=$(this).numberbox("options");
  144. if(parseFloat(s)!=s){
  145. if(_1c.prefix){
  146. s=$.trim(s.replace(new RegExp("\\"+$.trim(_1c.prefix),"g"),""));
  147. }
  148. if(_1c.suffix){
  149. s=$.trim(s.replace(new RegExp("\\"+$.trim(_1c.suffix),"g"),""));
  150. }
  151. if(_1c.groupSeparator){
  152. s=$.trim(s.replace(new RegExp("\\"+_1c.groupSeparator,"g"),""));
  153. }
  154. if(_1c.decimalSeparator){
  155. s=$.trim(s.replace(new RegExp("\\"+_1c.decimalSeparator,"g"),"."));
  156. }
  157. s=s.replace(/\s/g,"");
  158. }
  159. var val=parseFloat(s).toFixed(_1c.precision);
  160. if(isNaN(val)){
  161. val="";
  162. }else{
  163. if(typeof (_1c.min)=="number"&&val<_1c.min){
  164. val=_1c.min.toFixed(_1c.precision);
  165. }else{
  166. if(typeof (_1c.max)=="number"&&val>_1c.max){
  167. val=_1c.max.toFixed(_1c.precision);
  168. }
  169. }
  170. }
  171. return val;
  172. }});
  173. })(jQuery);