2013-03-27 226 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