gpt4 book ai didi

jquery - 即使用户尚未确认,链接仍在导航

转载 作者:搜寻专家 更新时间:2023-10-31 02:25:13 24 4
gpt4 key购买 nike

我的问题是,即使我在确认中点击了Cancel 按钮,链接仍然导航到它的目的地?如果用户单击确认框中的 cancel,我如何阻止链接导航到目的地?我只希望它在用户单击 OK 按钮时导航:

<a id='teachlogout' href='./teacherlogout.php'>Logout</a>
function logoutHandler() {
if (confirm("You are currently creating an Assessment, are you sure you want to logout?" + "\n" + "(Your current assessment details will be lost)" + "\n")) {
return true;
}
}

// logout link
$('#teachlogout').click(function() {
logoutHandler();
});

最佳答案

如果用户取消确认,您需要返回falseevent.preventDefault()。试试这个:

function logoutHandler() {
return confirm("You are currently creating an Assessment, are you sure you want to logout?" + "\n" + "(Your current assessment details will be lost)" + "\n");
}

// logout link
$('#teachlogout').click(logoutHandler);
// ^ Shorter version when you only need to call 1 function with no params

或者这个:

function logoutHandler(e) {
if (!confirm("You are currently creating an Assessment, are you sure you want to logout?" + "\n" + "(Your current assessment details will be lost)" + "\n")) {
e.preventDefault();
}
}

// logout link
$('#teachlogout').click(function(e) {
logoutHandler(e);
});

关于jquery - 即使用户尚未确认,链接仍在导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14231706/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com