- <a class='reduce' onclick="setAmount.reduce('#pamount')"
- href="javascript:void(0)">-</a>
- <input type='text' value='1' id='pamount' class='ino'
- onkeyup="setAmount.modify('#pamount')" />
- <a class='add' onclick="setAmount.add('#pamount')"
- href="javascript:void(0)">+</a>
- var setAmount={
- min:1,
- max:999,
- reg:function(x){
- return new RegExp("^[1-9]\\d*$").test(x);
- },
- amount:function(obj,mode){
- var x=$(obj).val();
- if (this.reg(x)){
- if (mode){
- x++;
- }else{
- x--;
- }
- }else{
- alert("请输入正确的数量!");
- $(obj).val(1);
- $(obj).focus();
- }
- return x;
- },
- reduce:function(obj){
- var x=this.amount(obj,false);
- if (x>=this.min){
- $(obj).val(x);
- }else{
- alert("商品数量最少为"+this.min);
- $(obj).val(1);
- $(obj).focus();
- }
- },
- add:function(obj){
- var x=this.amount(obj,true);
- if (x<=this.max){
- $(obj).val(x);
- }else{
- alert("商品数量最多为"+this.max);
- $(obj).val(999);
- $(obj).focus();
- }
- },
- modify:function(obj){
- var x=$(obj).val();
- if (x<this.min||x>this.max||!this.reg(x)){
- alert("请输入正确的数量!");
- $(obj).val(1);
- $(obj).focus();
- }
- }
- }
注意:如果min与max的值用function取得,比较的时候需要用parseInt转换才可。
转载于:https://blog.51cto.com/doubleqian/690268