2013-03-27 160 views
3

我有一些问题Ajax.ActionLink并确认对话框

@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()函数只返回 '' Ajax请求停止和我“删除”操作无法启动(所以它的工作原理应该如何工作)。但在我的示例功能MyConfirm()显示我MyConfirm对话框,但它也立即恢复为真,并且“删除”行动开始!如何处理?

回答

2

按:Javascript Alertify with return from confirm

Alertify是非阻塞代码,并将其在用户做出响应之前返回。使用提琴手或萤火虫查看用户选择和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; 
} 
+0

谢谢可惜它不工作... – 2013-03-27 15:37:41

+0

无论如何,我没有时间。我想我只是做 'click',它的工作肯定.. – 2013-03-27 15:38:46

+0

我更新了我的答案,你可以试试吗? – 2013-03-27 15:48:48

0

我还没有使用过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; 
} 
+0

是的,我知道它..感谢 – 2013-03-27 15:37:08