js 购买数量的加减修改
jquery.goods.js文件 $(function() { $('.goods-first #goods-text .goods-price').hover(function(){ $(this).toggleClass('curr'); }) $('#goodsDescription>.tabs>ul>li>a').click(function(){ $('#goodsDescription>.tabs>ul>li').removeClass('selected'); $(this).parent('li').addClass('selected'); var dataName = $(this).attr('dataname'); $("#goodsDescription>.goods-box").hide(); switch(dataName){ case 'aftermarket': $("html,body").animate({scrollTop:$("#goodsAftermarket").offset().top+1},200); break; case 'weibo': $("html,body").animate({scrollTop:$("#goodsWeibo").offset().top+1},200); break; case 'ask': $("html,body").animate({scrollTop:$("#goodsAsk").offset().top+1},200); break; case 'reviews': $("html,body").animate({scrollTop:$("#goodsReviews").offset().top+1},200); break; default: $("#goodsDescription>.goods-box").show(); $("html,body").animate({scrollTop:$("#goodsDescription").offset().top},200); } }) }) function buy(goodsId,gaId,parentId,once){ var number = Number($('#pamount').val()) > 0 ? Number($('#pamount').val()) : 1; $.post(siteUrl + 'ajax/cart_add_goods.html', {gid:goodsId,gaid:gaId,number:number,pid:parentId,once:once}, buyResponse, 'json'); } function buyResponse(result) { if(result.error > 0) { if(result.error == 1) { if(confirm('参数错误,请刷新后重新购买,立刻刷新?')){ window.location.reload; } }else if(result.error == 2) { cart_info_ajax() var modalTip = $('#modalBuyTip'); modalTip.children('.modal-body').html(result.message); $('#modalBuyTipLink').attr('href',siteUrl + 'buy/cart.html'); modalTip.modal({ backdrop:false, show:true }); } else { alert(result.message); } } else { if(result.once){ window.location.href = siteUrl + 'buy/checkout.html'; }else{ cart_info_ajax() var modalTip = $('#modalBuyTip'); modalTip.children('.modal-body').html(result.message); $('#modalBuyTipLink').attr('href',siteUrl + 'buy/cart.html').html('去购物车结算'); modalTip.modal({ backdrop:false, show:true }); } } }
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,goodsId) { var x = this.amount(obj, false); if(x >= this.min) { $(obj).val(x); } else { alert("请输入正确的数量!"); $(obj).val(1); $(obj).focus(); } }, add : function(obj,goodsId) { 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(); } } }
productsview.php文件 .......... <SCRIPT type=text/javascript src="images/jquery.goods.js"></SCRIPT> .......... <A class="reduce block fl" onclick="setAmount.reduce('#pamount')" href="javascript:void(0)">-</A> <INPUT id=pamount class=fl onKeyUp="setAmount.modify('#pamount')" value=1 type=text name=number style="height:30px"> <A class="add block fl" onClick="setAmount.add('#pamount')" href="javascript:void(0)">+</A> ..........
|