如何根据 ajax 调用中函数的结果使用 jQuery 取消单击事件?
我的目标是捕获网站上的所有点击事件,检查用户是否仍处于登录状态,然后让他们根据确定其当前登录状态的 ajax 调用的结果继续或取消其请求的操作。
我这样做是因为用户经常打开窗口并进行多个选择,运行报告并打开内部选项卡...允许超时的默认行为会导致用户丢失他们的工作。
因此,步骤 1:(拦截所有点击事件)
$(document).ready(function() {
$(document).click(function(event) { fncCheckLogin(event); });
});
步骤 2:(取消所有可点击对象的事件作为测试)
function fncCheckLogin(objEvent) { objEvent.preventDefault(); }
步骤 3:(检查登录状态)
$.ajax({
type:"POST",
url: "myURL",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType:"json",
complete: function(x){
if(x){
if (!isOpen) {
DoSomeStuff
}
);
isOpen = true;
testMe(objEvent);
}
} else {
if (isOpen) {
isOpen = false;
}
}
}
});
步骤 4:(如果登录已过期,则阻止该操作)
function testMe(objEvent) { if (isOpen) { objEvent.preventDefault(); }}
“ isOpen”变量只是标识注销用户脚本是否已运行。我试图直接从 ajax 调用访问 PreventDefault,但我将其移动以进行测试。
我认为我的问题是时机...如果我在 if 语句之前的步骤 4 中放置警报,则 PreventDefault 命令会起作用,但否则不会。就好像事件在我的 ajax 调用之前完成,但我不确定这就是问题所在。
任何帮助将不胜感激...谢谢!
My goal is to capture all the click events on in my website, check that the user is still logged in, then let them proceed or cancel their requested action based on the results of the ajax call which determines their current login status.
I'm going this due to users often having windows open with multiple selections made and reports run and internal tabs open... allowing the timeout's default behavior causes users to lose their work.
So, step 1: (intercept all click events)
$(document).ready(function() {
$(document).click(function(event) { fncCheckLogin(event); });
});
Step 2: (Cancel the event for all clickable objects as a test)
function fncCheckLogin(objEvent) { objEvent.preventDefault(); }
Step 3: (Check for Login Status)
$.ajax({
type:"POST",
url: "myURL",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType:"json",
complete: function(x){
if(x){
if (!isOpen) {
DoSomeStuff
}
);
isOpen = true;
testMe(objEvent);
}
} else {
if (isOpen) {
isOpen = false;
}
}
}
});
Step 4: (prevent the action if the login has expired)
function testMe(objEvent) { if (isOpen) { objEvent.preventDefault(); }}
The "isOpen" variable is just identifying whether or not the logged-out-user script has run. I was trying to access the preventDefault directly from the ajax call, but I moved it for testing stuff.
I think my problem is timing... if I put an alert in Step 4 prior to the if statement, the preventDefault command works, but otherwise it does not. It is as though the event is completing prior to my ajax call, but I'm not positive that's the problem.
Any help would be much appreciated... Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
AJAX 代表异步Javascript 和 XML。默认情况下,AJAX 调用不会阻塞,它们在后台运行,而其余代码则继续运行。因此,您的事件在 AJAX 调用完成之前完成是正确的。
jQuery 的 ajax() 方法允许您指定 async: false 作为参数之一来更改此行为,这应该会得到您想要的结果。
AJAX stands for asynchronous Javascript and XML. The AJAX calls don't block by default, they run in the background while the rest of your code moves on. So you're correct that your event is completing prior to the completion of your AJAX call.
jQuery's ajax() method lets you specify async: false as one of the parameters to change this behavior, which should get you what you want.
我能看到这个工作的唯一方法是使用 async:false。
但是,请注意,使用 async:false 时,“ajax”调用将挂起浏览器,直到它返回。
The only way I can see this working is with async:false.
But, be aware that with async:false the "ajax" call will hang the browser until it returns.
使用 jquery 事件委托。以及
async:false
。但正如 kingjiv 建议的那样, async:false 有其缺点,请使用必要的 UI 范例(光标到沙漏或 ajax 加载灯箱等)use jquery event delegation. along with
async:false
. But as kingjiv advised, async:false has its shortcomings, use necessary UI paradigms (cursor to hourglass or ajax loading light box etc)