用JavaScript实现购物车中数量的增减。当用户点击增加时,判断数量是否大于库存。若大于库存,不执行;反之,加1。但调试后发现并没有获取到最大库存量,总是提示:undefined。如图所示:
JavaScript代码如下:
- JScript code
<script type="text/javascript"> //点击+号图,数量+1 function Plus(obj) { [color=#FF0000]var ztb1 = document.getElementById('<%= Label1.ClientID %>'); [/color] alert(ztb1.value);// 提示:undefined obj.value = parseInt(obj.value) + 1; } //数量-1 function Reduce(obj) { if (obj.value > 1) { obj.value = obj.value - 1; } } //替换txtAmount文本框非整数的输入 //数据整个不合法时置1 function CheckValue(obj) { var v = obj.value.replace(/[^\d]/g, ''); if (v == '' || v == 'NaN') { obj.value = "1"; } else { obj.value = v; } } </script>
Product.aspx页面代码:
- HTML code
<dl class="amount clearfix"> <dt>数 量:</dt> <dd class="ctrl"> <a href="javascript:void(0);" class="reduce" id="areduce" runat="server">-</a> <asp:TextBox ID="txtAmount" runat="server" Text="1"></asp:TextBox> <a href="javascript:void(0)" class="add" id="aadd" runat="server">+</a> </dd> <dd class="level"> <span class="f33"> <asp:Literal ID="ltlClassifier4" runat="server"></asp:Literal> </span> </dd> <dd class="level"> <span class="f99">(库存 <em id="goods_stock"> <asp:Label ID="Label1" runat="server"></asp:Label></em> <asp:Literal ID="ltlClassifier5" runat="server"></asp:Literal>)</span> </dd></dl>
Product.aspx.cs页面代码:
- C# code
aadd.Attributes.Add("onclick", "Plus(" + txtAmount.ClientID + ")");areduce.Attributes.Add("onclick", "Reduce(" + txtAmount.ClientID + ")");
------解决方案--------------------------------------------------------
var ztb1 = document.getElementById('<%= Label1.ClientID %>');label对象没有获取到,所以undefined未定义噢
------解决方案--------------------------------------------------------
aadd.Attributes.Add("onclick", "Plus('" + txtAmount.ClientID + "')");
areduce.Attributes.Add("onclick", "Reduce('" + txtAmount.ClientID + "')");