Ajax.动作链接和确认对话框
本文关键字:确认 对话框 链接 Ajax | 更新日期: 2023-09-27 18:06:02
我有一些问题
@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();"})
my JavaScript function:
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"动作开始!如何处理呢?
根据:Javascript Alertify与返回从确认
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调用。但是你的函数目前没有返回任何东西。
所以Nicholas的答案可能是唯一正确的。
回复你的评论。假设你知道如何阻止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;
}
我没有使用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;
}