【问题标题】:Why is return confirm not working and inline onclick working为什么返回确认不起作用并且内联 onclick 起作用
【发布时间】:2017-11-08 09:57:12
【问题描述】:

我有一个带有 css 类 confirmDelete 的 asp net gridview 链接按钮,并且我使用 jquery 附加了一个点击事件,像这样

$('.confirmDelete').click(function (e) {
    return confirm('Are you sure you want to delete!!');
});

但它不起作用;无论我们点击什么,它都会显示确认对话框,它会停止按钮处理程序。

另一方面,像这样的内联点击事件有效

<asp:LinkButton runat="server" CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you sure you want to delete this?');" CssClass="btn btn-danger confirmDelete"></asp:LinkButton>

提前致谢

【问题讨论】:

  • 哦,是的。哎呀:)
  • 你应该使用事件代理来绑定事件,比如 jQuery 2.x 的$('.confirmDelete').on('click', function (e) { return confirm('Are you sure you want to delete!!'); });
  • 因为你在点击事件中的回调函数会将结果返回到深渊

标签: javascript jquery asp.net webforms


【解决方案1】:

我会试试这个:

$('.confirmDelete').click(function (e) {
    var confirm = window.confirm('Are you sure you want to delete!!');
    if (confirm) {
        // do all the things
    }
});

【讨论】:

    【解决方案2】:

    没有必要为此创建一个jquery函数,只需这样写:

    <asp:LinkButton runat="server" CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you sure you want to delete this?');" CssClass="btn btn-danger" ID="delete" OnClick="delete_Click"></asp:LinkButton>
    

    这将像这样工作: 当您首先单击链接按钮时,将出现一个确认对话框,如果您按 OK,则服务器事件将触发,否则服务器事件将不会触发

    【讨论】:

      【解决方案3】:

      你需要把点击事件放在$(document).ready()中。

      $(document).ready(function () {
          $('.confirmDelete').click(function (e) {
              return confirm('Are you sure you want to delete!!');
          });
      );
      

      【讨论】:

        猜你喜欢
        • 2021-12-31
        • 1970-01-01
        • 1970-01-01
        • 2014-12-22
        • 1970-01-01
        • 2020-01-20
        • 1970-01-01
        • 1970-01-01
        • 2021-10-10
        相关资源
        最近更新 更多