Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在asp:repeater中查找所选产品,因为我想在购物车中添加此所选产品_Javascript_C#_Asp.net_Asprepeater - Fatal编程技术网

Javascript 如何在asp:repeater中查找所选产品,因为我想在购物车中添加此所选产品

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

我想为我的在线商店制作购物车。我使用asp:repeater。我的问题是,我不知道选择了哪个产品,因为我不知道选择了哪个产品id。我使用javascript函数来实现这一点。当用户单击“添加到购物车”按钮时,在javascript函数中,此选定id被添加到数组中,但我不知道产品身份证

                        <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">&#9733; &#9733; &#9733; &#9733; &#9734;</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">&#9733; &#9733; &#9733; &#9733; &#9734;</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">&#9733; &#9733; &#9733; &#9733; &#9734;</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">&#9733; &#9733; &#9733; &#9733; &#9734;</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">&#9733; &#9733; &#9733; &#9733; &#9734;</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”);}