六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 27|回复: 0

js 购买数量的加减修改(电商购买数量)

[复制链接]

升级  79.33%

51

主题

51

主题

51

主题

秀才

Rank: 2

积分
169
 楼主| 发表于 2013-1-29 08:53:49 | 显示全部楼层 |阅读模式
jQuery.extend( {min : 1,reg : function(x) {jQuery('#J_Tip').html("");jQuery('#J_Tip').hide();return new RegExp("^[1-9]\\d*$").test(x);},amount : function(obj, mode) {var x = jQuery(obj).val();if (this.reg(parseInt(x))) {if (mode) {x++;} else {x--;}} else {jQuery('#J_Tip').html("<i class=\"ico\"></i>请输入正确的数量!");jQuery('#J_Tip').show();jQuery(obj).val(1);jQuery(obj).focus();}return x;},reduce : function(obj) {var x = this.amount(obj, false);if (parseInt(x) >= this.min) {jQuery(obj).val(x);} else {jQuery('#J_Tip').html("<i class=\"ico\"></i>商品数量最少为" + this.min+ "!");jQuery('#J_Tip').show();jQuery(obj).val(1);jQuery(obj).focus();}},add : function(obj) {var x = this.amount(obj, true);var max = jQuery('#nAmount').val();if (parseInt(x) <= parseInt(max)) {jQuery(obj).val(x);} else {jQuery('#J_Tip').html("<i class=\"ico\"></i>您所填写的商品数量超过库存!");jQuery('#J_Tip').show();jQuery(obj).val(max == 0 ? 1 : max);jQuery(obj).focus();}},modify : function(obj) {var x = jQuery(obj).val();var max = jQuery('#nAmount').val();if (!this.reg(parseInt(x))) {jQuery(obj).val(1);jQuery(obj).focus();jQuery('#J_Tip').html("<i class=\"ico\"></i>请输入正确的数量!");jQuery('#J_Tip').show();return;}var intx = parseInt(x);var intmax = parseInt(max);if (intx < this.min) {jQuery('#J_Tip').html("<i class=\"ico\"></i>商品数量最少为" + this.min+ "!");jQuery('#J_Tip').show();jQuery(obj).val(this.min);jQuery(obj).focus();} else if (intx > intmax) {jQuery('#J_Tip').html("<i class=\"ico\"></i>您所填写的商品数量超过库存!");jQuery('#J_Tip').show();jQuery(obj).val(max == 0 ? 1 : max);jQuery(obj).focus();}}}); 使用方式:<span class="li_hd">购买数量:</span>        <a class="num_oper num_min" href="javascript:jQuery.reduce('#J_Amount');">-</a>        <input name="J_Amount" id="J_Amount" class="input_txt" type="text" maxlength="6" value="1" onkeyup="jQuery.modify('#J_Amount');"/><a class="num_oper num_plus" href="javascript:jQuery.add('#J_Amount')">+</a>        <p class="caution_tips" id="J_Tip" style="display:none;"></p><input id="nAmount" type="hidden" value="10"/>
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表