1. <a class='reduce' onclick="setAmount.reduce('#pamount')" 
  2. href="javascript:void(0)">-</a>
  3. <input type='text' value='1' id='pamount' class='ino' 
  4. onkeyup="setAmount.modify('#pamount')" /> 
  5. <a class='add' onclick="setAmount.add('#pamount')" 
  6. href="javascript:void(0)">+</a> 

 

 
  
  1. var setAmount={ 
  2. min:1, 
  3. max:999, 
  4. reg:function(x){ 
  5. return new RegExp("^[1-9]\\d*$").test(x); 
  6. }, 
  7.  
  8. amount:function(obj,mode){
  9. var x=$(obj).val(); 
  10. if (this.reg(x)){
  11. if (mode){ 
  12. x++; 
  13. }else
  14. x--; 
  15. }else
  16. alert("请输入正确的数量!");  
  17. $(obj).val(1);     
  18. $(obj).focus(); 
  19. return x; 
  20. }, 
  21.  
  22. reduce:function(obj){ 
  23. var x=this.amount(obj,false);  
  24. if (x>=this.min){         
  25. $(obj).val(x); 
  26. }else
  27. alert("商品数量最少为"+this.min); 
  28. $(obj).val(1);   
  29. $(obj).focus(); 
  30. }, 
  31.  
  32. add:function(obj){  
  33. var x=this.amount(obj,true); 
  34. if (x<=this.max){ 
  35. $(obj).val(x); 
  36. }else
  37. alert("商品数量最多为"+this.max);     
  38. $(obj).val(999);   
  39. $(obj).focus(); 
  40. }, 
  41.  
  42. modify:function(obj){  
  43. var x=$(obj).val(); 
  44. if (x<this.min||x>this.max||!this.reg(x)){     
  45. alert("请输入正确的数量!");   
  46. $(obj).val(1);   
  47. $(obj).focus(); 

注意:如果min与max的值用function取得,比较的时候需要用parseInt转换才可。