我有一个小的JQuery片段,在按下“添加到购物车”按钮时执行。该页面基本上有多个产品,带有单独的添加到购物车按钮。因此,当按下此按钮时,将创建一个循环,用于评估每个产品数量,并仅添加按下“添加到购物车”按钮的项目。我想删除此代码段中的循环。如果需要任何其他详细信息,请与我们联系。
<script type="text/javascript">
$('.addtocartbtn a').on('click', function (e) {
var link = this;
var itemname = $('#Content1_lblName').html().replace(/Adopt a /g, '');
var itemregex = new RegExp(' ' + itemname, 'g');
var itemlevel = $(link).parent().parent().prev('h3').html().replace(itemregex, '');
var box = link.parentNode.previousSibling;
var quant = box.id;
while (!quant) {
box = box.previousSibling;
quant = box.id;
}
if (box.value < 1 || /\D/.test(box.value)) {
$(box).next().next().css("display", "block");
} else {
$('.quantity').each(function () {
if ($(this).attr('id') != quant) $(this).val('');
else {
dataLayer.push({
'gtmCategory': 'Page Type',
'gtmAction': 'Species Adoption Add to Cart',
'gtmLabel': itemname + " Set of (" + $(this).val() + ") (" + itemlevel.toString().substr(level.toString().indexOf('$'), 4).trim() + ")",
'event': 'gtmEvent'
});
}
});
__doPostBack('content1$btnAddToCart', '');
}
(e.preventDefault) ? e.preventDefault() : e.returnValue = false;
}); </script>
附上页面的样子。使用当前代码段,当按下一个按钮时,所有项目都会添加到购物车中。但我的实际要求是只有选定的项目必须添加到购物车。
以下是HTML标记示例:
<asp:Content ID="Content1" ContentPlaceHolderID="Content1" runat="Server">
<asp:ScriptManager ID="scriptManager" runat="server" AsyncPostBackTimeout="300" />
<asp:UpdatePanel runat="server" ID="updatePanel">
<ContentTemplate>
<div class="span9">
<a id="breadcrumb" runat="server" href="https://www.google.com/" class="hdr-important"><strong>Sample breadcrumb</strong></a>
<div class="row">
<div class="span4 push-right">
<div class="break-gutter-horiz gutter-bottom-2">
<img runat="server" id="imgMain" alt="" src="" width="320" height="213" />
</div>
</div>
<div class="span5 no-gutter gutter-bottom-fixed-2">
<div class="wysiwyg break-gutter-horiz more-less">
<p id="lblFacts" runat="server"></p>
</div>
</div>
</div>
<asp:Repeater ID="itemlist" runat="server">
<ItemTemplate>
<div class="accordion gutter-bottom-4">
<div class="accordion-item expanded gutter-bottom-fixed-4">
<div class="break-gutter-horiz">
<asp:HiddenField ID="itemid" runat="server" />
<asp:Literal ID="mostPopularDiv" runat="server"></asp:Literal>
<h3 id="itemnameheader" class="hdr hdr-tertiary">
<asp:Literal ID="itemleveltitle" runat="server"></asp:Literal></h3>
<div class="accordion-content show">
<asp:Literal ID="itemDetailsLink" runat="server"></asp:Literal>
<asp:Literal ID="itemDetailsDivId" runat="server"></asp:Literal>
<asp:Literal ID="itemDetailsContent" runat="server"></asp:Literal>
</div>
<label for="quantity1"><strong>Quantity</strong></label>
<asp:DropDownList ID="quantitydropdown" runat="server" Enabled="true" >
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
<asp:ListItem Value="5">5</asp:ListItem>
<asp:ListItem Value="6">6</asp:ListItem>
<asp:ListItem Value="7">7</asp:ListItem>
<asp:ListItem Value="8">8</asp:ListItem>
<asp:ListItem Value="9">9</asp:ListItem>
<asp:ListItem Value="10">10</asp:ListItem>
<asp:ListItem
</asp:DropDownList>
<span class="addtocartbtn"><a class="btn-orange btn-simple btn-break-block" href="#">Add to Cart</a></span>
</div>
</div>
</div>
</ItemTemplate>
</asp:Repeater>