【问题标题】:Ajax.ActionLink and confirm dialogAjax.ActionLink 和确认对话框
【发布时间】:2013-03-17 17:27:00
【问题描述】:

我有一些问题

@Ajax.ActionLink

我想显示确认对话框,是的,我知道我可以这样做:

@Ajax.ActionLink("Do it!", "Delete", new AjaxOptions(){ Confirm = "Are you sure?" });

但我想拥有自己的 MyConfirm 对话框
我使用 alertify

所以我的代码是:

 @Ajax.ActionLink("Do it!", "Delete", new AjaxOptions(){  OnBegin="return MyConfirm();"})

我的 JavaScript 函数:

function MyConfirm() {
        alertify.confirm("Do you really want to do that??", function (e) {
           if (e) return true;
           else return false;
        });    
 }

但如果我在我的 MyConfirm() 函数中只返回 'false' Ajax 请求停止并且我的“Delete”操作不会开始(所以它应该如何工作)。但在我的示例函数 MyConfirm() 中显示我的 MyConfirm 对话框,但它也会立即返回 true 并且“Delete”操作开始!怎么处理?

【问题讨论】:

    标签: c# javascript asp.net ajax asp.net-mvc-4


    【解决方案1】:

    根据:Javascript Alertify with return from confirm

    Alertify 是一个非阻塞代码,它会在用户做出响应之前返回。使用 fiddler 或 firebug 查看用户选择和 ajax 请求的时间线。

    function MyConfirm() {
            alertify.confirm("Do you really want to do that??", function (e) {
               if (e) alert('after they pressed confirm, but ajax is already sent');
               else alert('after they pressed confirm, but ajax is already sent');
            });
            // no return here
     }
    

    根据http://yassershaikh.com/how-to-use-onbegin-method-with-ajax-beginform/ 返回false 应该取消Ajax 调用。但您的函数目前不返回任何内容。

    所以尼古拉斯的答案可能是唯一正确的。

    回应您的评论。假设您知道如何阻止 js 的执行(这是一件可怕的事情!而且您不应该这样做!)这将为您解决问题:

    // this tells us if use made a choice
    var userClicked = false;
    // this is for user's choice
    var userChoice;
    
    function MyConfirm() {
        alertify.confirm("Do you really want to do that??", function (e) {
            // mark that user clicked
            userClicked = true;
            if (e) {
                userChoice = true;
            } else {
                userChoice = false;
            }
        });
    
        // Put your delay code here 
        // userClicked tells you that user made a choice
        // Remember that setTimout will fail here as it's a fork, not a blocking function
        // you will have to do some crazy while loops that will make unicorns cry
    
        userClicked = false;
        return userChoice;
    }
    

    【讨论】:

    • 反正我没时间。我想我只是做<a onclick="MyJavaScriptFunctionWIthConfirm();">click</a> 并且它确实有效..
    • 我知道!在我对stackoverflow提出问题之前,我尝试了所有方法......但无论如何谢谢......看看我的代码。你看到我的和你的有什么不同吗?除了警觉? Ajax 总是发送.. 无论我做什么.. 我已经尝试了一些时间延迟,但它也不起作用
    【解决方案2】:

    我没用过alertify,但是从方法签名来看,我推测alertify.confirm会在用户稍后关闭弹窗时立即返回并运行回调方法。

    这意味着你的MyConfirm方法也立即返回,如果没有返回false,则开始ajax调用。

    您可以通过始终从 MyConfirm 返回 false 并仅在您的 alertify.confirm 回调函数中进行 ajax 调用来解决此问题:

    function MyConfirm() {
        alertify.confirm("Do you really want to do that??", function (e) {
    
           // this actually makes the ajax call if required
           if (e) doAjaxCall();
        });    
    
    
        return false; 
     }
    

    【讨论】:

      猜你喜欢
      • 2011-02-27
      • 2012-03-25
      • 1970-01-01
      • 1970-01-01
      • 2011-05-24
      • 2016-02-02
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多