jquery控制input只能输入金额

例子表单:

[html]  view plain  copy
  1. <input type="text" name="input1" id="input1" value=""  />  
  2. <input type="text" name="input2" id="input2" value=""  />  
  3. <input type="text" name="input3" id="input3" value=""  />  

js代码:

[javascript]  view plain  copy
  1. $(document).ready(function(){  
  2.     bindKeyEvent($("#input1"));  
  3.     bindKeyEvent($("#input2"));  
  4.     bindKeyEvent($("#input3"));  
  5. });  
  6. function bindKeyEvent(obj){  
  7.     obj.keyup(function () {  
  8.         var reg = $(this).val().match(/\d+\.?\d{0,2}/);  
  9.         var txt = '';  
  10.         if (reg != null) {  
  11.             txt = reg[0];  
  12.         }  
  13.         $(this).val(txt);  
  14.     }).change(function () {  
  15.         $(this).keypress();  
  16.         var v = $(this).val();  
  17.         if (/\.$/.test(v))  
  18.         {  
  19.             $(this).val(v.substr(0, v.length - 1));  
  20.         }  
  21.     });  
  22. }  

猜你喜欢

转载自blog.csdn.net/sinat_31177681/article/details/80174790