Javascript 如何在asp:repeater中查找所选产品,因为我想在购物车中添加此所选产品
我想为我的在线商店制作购物车。我使用asp:repeater。我的问题是,我不知道选择了哪个产品,因为我不知道选择了哪个产品id。我使用javascript函数来实现这一点。当用户单击“添加到购物车”按钮时,在javascript函数中,此选定id被添加到数组中,但我不知道产品身份证Javascript 如何在asp:repeater中查找所选产品,因为我想在购物车中添加此所选产品,javascript,c#,asp.net,asprepeater,Javascript,C#,Asp.net,Asprepeater,我想为我的在线商店制作购物车。我使用asp:repeater。我的问题是,我不知道选择了哪个产品,因为我不知道选择了哪个产品id。我使用javascript函数来实现这一点。当用户单击“添加到购物车”按钮时,在javascript函数中,此选定id被添加到数组中,但我不知道产品身份证 <div class="col-lg-4 col-md-6 mb-4"> <div clas
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100">
<!-- add repeater -->
<a href="#">
<asp:Image ID="Image1" runat="server" class="card-img-top" ImageUrl='<%# Eval("imageUrl") %>' Style="height: 100px; width: 250px;" /></a>
<!--
<img class="card-img-top" src="http://placehold.it/700x400" alt=""></a>
-->
<div class="card-body">
<h4 class="card-title" runat="server">
<a href="#">
<asp:Label ID="nameMedicine" runat="server" Text='<%# Eval("name") %>' /></a>
<!--<a href="#"><%#Eval("name") %></a>-->
</h4>
<h5>price <%#Eval("price") %></h5>
<p class="card-text"><%#Eval("description") %></p>
</div>
<div class="card-footer">
<small class="text-muted">★ ★ ★ ★ ☆</small>
</div>
<asp:Button ID="addItems" OnClientClick="addToCart()" ClientIDMode="Predictable" runat="server" Text="Add To Cart" Style="color: white; background-color: #6dabe4;" />
</div>
</div>
</ItemTemplate>
</asp:Repeater>
<script>
function addToCart() {
alert(document.getElementById("nameMedicine"));
}
</script>
设计器文件
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100">
<!-- add repeater -->
<a href="#">
<asp:Image ID="Image1" runat="server" class="card-img-top" ImageUrl='<%# Eval("imageUrl") %>' Style="height: 100px; width: 250px;" /></a>
<!--
<img class="card-img-top" src="http://placehold.it/700x400" alt=""></a>
-->
<div class="card-body">
<h4 class="card-title" runat="server">
<a href="#">
<asp:Label ID="nameMedicine" runat="server" Text='<%# Eval("name") %>' /></a>
<!--<a href="#"><%#Eval("name") %></a>-->
</h4>
<h5>price <%#Eval("price") %></h5>
<p class="card-text"><%#Eval("description") %></p>
</div>
<div class="card-footer">
<small class="text-muted">★ ★ ★ ★ ☆</small>
</div>
<asp:Button ID="addItems" OnClientClick="addToCart()" ClientIDMode="Predictable" runat="server" Text="Add To Cart" Style="color: white; background-color: #6dabe4;" />
</div>
</div>
</ItemTemplate>
</asp:Repeater>
<script>
function addToCart() {
alert(document.getElementById("nameMedicine"));
}
</script>
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100">
<!-- add repeater -->
<a href="#">
<asp:Image ID="Image1" runat="server" class="card-img-top" ImageUrl='<%# Eval("imageUrl") %>' Style="height: 100px; width: 250px;" /></a>
<!--
<img class="card-img-top" src="http://placehold.it/700x400" alt=""></a>
-->
<div class="card-body">
<h4 class="card-title" runat="server">
<a href="#">
<asp:Label ID="nameMedicine" runat="server" Text='<%# Eval("name") %>' /></a>
<!--<a href="#"><%#Eval("name") %></a>-->
</h4>
<h5>price <%#Eval("price") %></h5>
<p class="card-text"><%#Eval("description") %></p>
</div>
<div class="card-footer">
<small class="text-muted">★ ★ ★ ★ ☆</small>
</div>
<asp:Button ID="addItems" OnClientClick="addToCart()" ClientIDMode="Predictable" runat="server" Text="Add To Cart" Style="color: white; background-color: #6dabe4;" />
</div>
</div>
</ItemTemplate>
</asp:Repeater>
<script>
function addToCart() {
alert(document.getElementById("nameMedicine"));
}
</script>
价格
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100">
<!-- add repeater -->
<a href="#">
<asp:Image ID="Image1" runat="server" class="card-img-top" ImageUrl='<%# Eval("imageUrl") %>' Style="height: 100px; width: 250px;" /></a>
<!--
<img class="card-img-top" src="http://placehold.it/700x400" alt=""></a>
-->
<div class="card-body">
<h4 class="card-title" runat="server">
<a href="#">
<asp:Label ID="nameMedicine" runat="server" Text='<%# Eval("name") %>' /></a>
<!--<a href="#"><%#Eval("name") %></a>-->
</h4>
<h5>price <%#Eval("price") %></h5>
<p class="card-text"><%#Eval("description") %></p>
</div>
<div class="card-footer">
<small class="text-muted">★ ★ ★ ★ ☆</small>
</div>
<asp:Button ID="addItems" OnClientClick="addToCart()" ClientIDMode="Predictable" runat="server" Text="Add To Cart" Style="color: white; background-color: #6dabe4;" />
</div>
</div>
</ItemTemplate>
</asp:Repeater>
<script>
function addToCart() {
alert(document.getElementById("nameMedicine"));
}
</script>
★ ★ ★ ★ ☆
函数addToCart(){
警报(document.getElementById(“nameMedicine”);
}
您可以将产品/药品Id作为参数发送给函数addToCart。
见下文:
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100">
<!-- add repeater -->
<a href="#">
<asp:Image ID="Image1" runat="server" class="card-img-top" ImageUrl='<%# Eval("imageUrl") %>' Style="height: 100px; width: 250px;" /></a>
<!--
<img class="card-img-top" src="http://placehold.it/700x400" alt=""></a>
-->
<div class="card-body">
<h4 class="card-title" runat="server">
<a href="#">
<asp:Label ID="nameMedicine" runat="server" Text='<%# Eval("name") %>' /></a>
<!--<a href="#"><%#Eval("name") %></a>-->
</h4>
<h5>price <%#Eval("price") %></h5>
<p class="card-text"><%#Eval("description") %></p>
</div>
<div class="card-footer">
<small class="text-muted">★ ★ ★ ★ ☆</small>
</div>
<asp:Button ID="addItems" OnClientClick="addToCart()" ClientIDMode="Predictable" runat="server" Text="Add To Cart" Style="color: white; background-color: #6dabe4;" />
</div>
</div>
</ItemTemplate>
</asp:Repeater>
<script>
function addToCart() {
alert(document.getElementById("nameMedicine"));
}
</script>
<asp:Button ID="addItems" OnClientClick="addToCart(<%# Eval("ProductId") %>)" ClientIDMode="Predictable" runat="server" Text="Add To Cart" Style="color: white; background-color: #6dabe4;" />
将ProductId参数放入OnClientClick=“addToCart()”
函数中,如addToCart('1')
我将传入e
作为addToCart()的参数,然后您可以使用e.target来知道单击了哪个元素。OnClientClick='addToCart()'不起作用。我什么也得不到。这里的名称是我的主键。当我编写这样的代码时,不要调用javascript函数OnClientClick='addToCart()'。OnClientClick='addToCart()'不起作用。我什么也得不到。这里的名称是我的主键。当我编写像OnClientClick='addToCart()'这样的代码时,javascript函数不会调用。请尝试此html按钮和javascript函数<代码>添加到购物车函数addToCart(args){alert(args.getAttribute(“value”);}