【问题标题】:Actionlink with confirmation dialog带有确认对话框的操作链接
【发布时间】:2016-02-19 17:22:42
【问题描述】:

我想在链接打开之前显示一条确认消息。但是,尽管如此,尽管单击取消或关闭对话框,仍会遵循该链接。请帮助我,因为我被困住了。

<div style="float: left; width: 40px; height: 10px; "> @Html.ActionLink("-Pg", "SupprimerPage", "Section", new { pageId = @item.Id }, new { onclick = "ConfirmerSuppressionPage(event);", @class = "editLink", style = "width:30px" })</div>

Javascript:

 function ConfirmerSuppressionPage(event) {
    var x = confirm("Êtes-vous sûr de vouloir supprimer cette page?");
    console.log(x);
    if (x == null) {
        event.preventDefault();
        return false;
    }

    if (x == true) {

        return true;
    }
    else {
        event.preventDefault();
        event.stopPropagation();
    }
}

【问题讨论】:

  • 建议你去掉onclick,直接使用$('.editLink).click(function() { return confirm("Êtes-vous ....?"); })
  • @StephenMuecke:尽管点击了“取消”,但它仍然遵循 URL。
  • @tabby 你在测试什么浏览器?在 chrome 和 IE 11 中,一切都按预期工作。
  • 对我来说很好(你的代码和我的代码)。使用您的代码,您只需要一行代码 - return confirm("Êtes-vous....?");
  • 浏览器控制台有错误吗?

标签: javascript asp.net-mvc confirmation


【解决方案1】:

Confirm 是一个 JavaScript 对话框,它将返回 truefalse。并且在用户不点击对话框的YesNo 按钮之前,JavaScript 不会执行下一行。

Any how Confirm will return you True or False, and now you have to deal with it.

【讨论】:

  • 我建议你再读一遍这个问题。 OP 正在检查返回值。
【解决方案2】:
var result = confirm("Êtes-vous sûr de vouloir supprimer cette page?");
if (result == true) {
    x = "You pressed OK!";
} else {
    x = "You pressed Cancel!";
}

【讨论】:

    【解决方案3】:

    这可能无济于事,但我遇到了某些浏览器不尊重取消按钮的类似问题,并创建了一个替换功能来解决它​​:

    function showConfirm(str) {
        if (confirm(str) == false) {
            if (typeof (event) != "undefined") {
                event.returnValue = false;
            }
    
            return false;
        }
        else {
            return true;
        }
    }
    

    然后在您的 ConfirmerSuppressionPage 函数中,将对 confirm 的调用替换为 showConfirm

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-16
      • 2011-07-11
      • 2015-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-29
      • 1970-01-01
      相关资源
      最近更新 更多