gpt4 book ai didi

javascript - 单击“确定”按钮时 jquery 中的问题

转载 作者:行者123 更新时间:2023-11-27 23:56:02 25 4
gpt4 key购买 nike

我编写了 jquery 和 html css,如下代码行

    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type="text/javascript">

$(function () {
resetTimer();
document.onkeypress = resetTimer;
document.onmouseover = resetTimer;

function logout() {
var t1 = setTimeout(reallyLogout, 60000);
$(".popup-form").css("display", "block");
}
var t;
function resetTimer() {
clearTimeout(t);
t = setTimeout(logout, 60000); // Logout in 10 minutes
}
function reallyLogout() {
debugger
$(".popup-form").css("display", "none");
location.href = '../login/login.aspx';
}
function cancel() {
clearTimeout(t1);
resetTimer();
$(".popup-form").css("display", "none");
}
});

</script>

<style type="text/css">
.popup-form {
display:inline-block;
padding:40px;
background-color:white;
border:1px solid black;
position:fixed;
left:40%;
top:35%;
display:none;
z-index:99999999999999999;
}
</style>

<body>
<div class="popup-form">
<div class="popup-inner-form">
<h4>Confirm</h4>
You will be logged out after 60 seconds
<input id="okbtn" value="Ok" type="submit" onclick="return reallyLogout();" />
<input id="cancelbtn" value="Cancel" type="submit" onclick="return cancel();" />
</div>
</div>
</body>

我创建了自定义表单...当用户闲置 60 秒时显示。实际上我们的主要目的是在 60 秒后显示弹出窗口。如果用户不按任何按钮(确定/取消),则应将其重定向到登录页面。现在的问题是,当显示弹出窗口并单击“确定”按钮时,它不会将其重定向到登录页面...请帮助我解决代码中的问题!!!

最佳答案

首先,由于您使用的是立即执行的函数,因此您应该传递 jQuery 作为参数,以便它在函数范围内处于事件状态。

(function ($) { --> })(jQuery);

您应该在即时函数作用域内附加 onclick 事件,以便以 reallyLogout 的身份访问函数,因为您无权访问即时函数调用作用域之外的函数,因为它只执行一次在这个范围内..

t1 应在函数的全局范围内声明,以便您可以从任何嵌套函数调用中的任何位置重置它 var t1;

这是一个工作示例..我将时间设置为 4 秒

   (function ($) {
resetTimer();
var t1; // set this in global scope
document.onkeypress = resetTimer;
document.onmouseover = resetTimer;

function logout() {
t1 = setTimeout(reallyLogout, 4000);
$(".popup-form").css("display", "block");
}
var t;
function resetTimer() {
clearTimeout(t);
t = setTimeout(logout, 4000); // Logout in 10 minutes
}
function reallyLogout() {
// debugger
console.log($(".popup-form"));
$(".popup-form").css("display", "none");
location.href = '../login/login.aspx';
}
function cancel() {
clearTimeout(t1);
resetTimer();
$(".popup-form").css("display", "none");
}

$("#okbtn").click(function(){reallyLogout();});
$("#cancelbtn").click(function(){cancel();});
})(jQuery);
 .popup-form {
display:inline-block;
padding:10px;
background-color:white;
border:1px solid black;
position:fixed;
left:10%;
top:35%;
display:none;
z-index:99999999999999999;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<div class="popup-form">
<div class="popup-inner-form">
<h4>Confirm</h4>
You will be logged out after 60 seconds
<input id="okbtn" value="Ok" type="submit" />
<input id="cancelbtn" value="Cancel" type="submit" />
</div>
</div>
</body>

关于javascript - 单击“确定”按钮时 jquery 中的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32263791/

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