Javascript 单击ok按钮时jquery中的问题

Javascript 单击ok按钮时jquery中的问题,javascript,jquery,Javascript,Jquery,我有WriitenjQuery和HTMLCSS,如下代码行 <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { resetTimer(); document.onkeypress

我有WriitenjQuery和HTMLCSS,如下代码行

    <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>

$(函数(){
重置计时器();
document.onkeypress=重置计时器;
document.onmouseover=重置计时器;
函数注销(){
var t1=设置超时(reallyLogout,60000);
$(“.popup form”).css(“显示”、“块”);
}
变量t;
函数resetTimer(){
清除超时(t);
t=setTimeout(注销,60000);//10分钟后注销
}
函数reallyLogout(){
调试器
$(“.popup form”).css(“显示”、“无”);
location.href='../login/login.aspx';
}
函数cancel(){
清除超时(t1);
重置计时器();
$(“.popup form”).css(“显示”、“无”);
}
});
.弹出式表格{
显示:内联块;
填充:40px;
背景色:白色;
边框:1px纯黑;
位置:固定;
左:40%;
最高:35%;
显示:无;
z指数:999999999999;
}
证实
您将在60秒后注销
我已经创建了自定义表单。。。 当用户空闲60秒时显示。实际上,我们的主要目的是在60秒后显示弹出窗口。若用户不按任何按钮(确定/取消),则应将其重定向到登录页面。 现在的问题是,当弹出窗口显示并单击ok按钮时,它不会将其重定向到登录页面。。。请帮我整理一下代码中的问题

尝试使用

<input type="button">

而不是

<input type="submit">

首先,由于您使用的是即时执行函数,因此应将
jQuery
作为参数传递,以便它在函数范围内处于活动状态

(函数($){
-->
})(jQuery)

您应该在即时函数范围内附加事件onclick,以便以
realllogout
的方式访问函数,因为您无法访问即时函数调用范围外的函数,因为它在此范围内只执行一次

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

这是一个有效的例子。。我已经计时4秒了

(函数($){
重置计时器();
var t1;//在全局范围内设置此值
document.onkeypress=重置计时器;
document.onmouseover=重置计时器;
函数注销(){
t1=设置超时(reallyLogout,4000);
$(“.popup form”).css(“显示”、“块”);
}
变量t;
函数resetTimer(){
清除超时(t);
t=setTimeout(注销,4000);//10分钟后注销
}
函数reallyLogout(){
//调试器
console.log($(“.popup form”);
$(“.popup form”).css(“显示”、“无”);
location.href='../login/login.aspx';
}
函数cancel(){
清除超时(t1);
重置计时器();
$(“.popup form”).css(“显示”、“无”);
}
$(“#okbtn”)。单击(函数(){realllogout();});
$(“#cancelbtn”)。单击(函数(){cancel();});
})(jQuery)
。弹出式窗体{
显示:内联块;
填充:10px;
背景色:白色;
边框:1px纯黑;
位置:固定;
左:10%;
最高:35%;
显示:无;
z指数:999999999999;
}

证实
您将在60秒后注销

脚本运行时,不会加载dom元素。 将脚本位置更改为刚好在之前


.弹出式表格{
显示:内联块;
填充:40px;
背景色:白色;
边框:1px纯黑;
位置:固定;
左:40%;
最高:35%;
显示:无;
z指数:999999999999;
}
证实
您将在60秒后注销
//你的剧本
删除onclick=“return realllogout();”&&onclick=“return cancel();”中的return,并剪切realllogout()&&cancel()实现并粘贴到$(function(){})之外

如果您使用的是$(“#okbtn”)。请单击(function(){});然后可以在$(函数(){})中使用它;在这种情况下,还可以删除HTML中的onclick函数


如果您按照您的结构进行微小的更改,那么在$(function(){})中将函数声明为全局函数;i、 e reallyLogout=function(){///要执行的操作//}

应在以下方法中添加括号:

t = setTimeout(logout(), 60000); // Logout in 10 minutes

var t1 = setTimeout(reallyLogout(), 60000);

然后,它就会起作用。此外,我还测试了。

以使用文档中定义的函数,该函数已从块外就绪。创建从外部作为全局访问所需的函数。请参阅下面的代码片段。我修改了从外部访问所需的reallyLogoutcancel功能。

JS:

   $(function () {   
     resetTimer();
     document.onkeypress = resetTimer;
     document.onmouseover = resetTimer;
     var t1;
     function logout() {
         t1 = setTimeout(reallyLogout, 60000);
         $(".popup-form").css("display", "block");
     }
     var t;
     function resetTimer() {
         clearTimeout(t);
         t = setTimeout(logout, 60000); // Logout in 10 minutes
     }

    //declared the reallyLogout as a global function
    reallyLogout= function () {

         debugger
         $(".popup-form").css("display", "none");
         location.href = '../login/login.aspx';
     };
     cancel= function () {
         clearTimeout(t1);
         resetTimer();
         $(".popup-form").css("display", "none");
     }


 });

为什么您有
onclick=“return realllogout();”
onclick=“return cancel();“
您不应该删除
return而只拥有这些函数?如果您删除return,它将起作用。为什么,因为最初加载文档时,它将调用resetTimer(),从那里它将达到setTimeout(注销,60000)。在给定的时间后,我将去注销。在再次注销时,您将设置Timeout(),从那里它指向reallyLogout()。要避免这些循环,只需使用$(“#Custom ID”).click(function(){required code To execute on click;});您无法从外部访问在文档内部创建的函数。如果您想从外部调用该函数,那么该函数应该在