Jquery 确认不工作

Jquery confirm not working

我有以下 <asp:Label.../> :

<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>' ForeColor="Desktop" /></h4>

和这个 html <button ... > 控件:

<button id="btnAddToCart" runat="server" class="btn btn-1 btn-1c center" onserverclick="btnAddToCart_ServerClick" onclick="Confirm()"><span class="icon-shopping-cart"></span>&nbsp;&nbsp;add to cart</button>

P.S: 我使用的是普通的 <button> 而不是 <asp:Button../> 控件因为我正在使用 <span class="icon=shopping-cart">.. 使用 asp 按钮控件不允许在其中嵌套范围 class。

现在, 我希望在单击按钮时,警报应该带有选项 YES 或 NO,因为它出现在 confirm("") ..

我编写了以下脚本:

<script type="text/javascript">
        $('#btnAddToCart').click(function () { //i have also tried $('.btnAddCart')...
            var productName = $('#lblName').val()
            confirm("Are you sure you want to add " + productName + "into your cart ?");
        });
    </script>

P.S: 我已经将 script 嵌入到 body 标签中(即 ContentPlaceHolder) 脚本有什么问题..还有我应该用哪种方式弹出 confirm() ..?

此外,我在 ul-li 列表中嵌套了 labelbutton 控件!

将您的按钮更改为:

<button id="btnAddToCart" ClientIdMode="Static"

您的 jquery 不起作用,因为您的按钮 ID 不是 "btnAddToCart",它是所有命名容器的串联 + "btnAddToCart"。这是默认的 ClientIdMode。你想要静态的,这基本上是在告诉 asp.net "Don't worry. I know what I'm doing. There will only ever be one of this control on a page with this id."

有关更多信息,请参阅:https://msdn.microsoft.com/en-us/library/system.web.ui.clientidmode(v=vs.110).aspx

使用 <asp:LinkButton.../> 比使用普通 <button.../> 控件更强大...

而不是使用:

<button id="btnAddToCart" runat="server" class="btn btn-1 btn-1c center" onserverclick="btnAddToCart_ServerClick" onclick="Confirm()"><span class="icon-shopping-cart"></span>&nbsp;&nbsp;add to cart</button>

您可以使用:

<asp:LinkButton ID="lnkButton" runat="server" CssClass="btn btn-1 btn-1c center" OnClientClicke="return confirm('Are you sure you want to add this product?')" OnClick="lnkButton_Click"><span class="icon-shopping-cart"></span>Add to Cart</asp:LinkButton>