Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果在JQuery模式对话框中单击escape键,如何将用户发送到主页_Javascript_Jquery_Asp.net Mvc - Fatal编程技术网

Javascript 如果在JQuery模式对话框中单击escape键,如何将用户发送到主页

Javascript 如果在JQuery模式对话框中单击escape键,如何将用户发送到主页,javascript,jquery,asp.net-mvc,Javascript,Jquery,Asp.net Mvc,在模式对话框中按escape键时,是否有方法跳转到web主页?下面是我正在使用的模式对话框: <script> $(function () { $("#dialog-login-message").dialog({ modal: true, buttons: { Ok: function

在模式对话框中按escape键时,是否有方法跳转到web主页?下面是我正在使用的模式对话框:

        <script>
            $(function () {
                $("#dialog-login-message").dialog({
                    modal: true,
                    buttons: {
                        Ok: function () {
                            document.location.href = "/";
                            $(this).dialog("close");
                        }
                    }
                });
            });
        </script>

        <div id="dialog-login-message" title="Login Required">
            <p>
                <span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
                You are not logged into this website. You need to be logged into this website to use the demo.;
            </p>
        </div>

$(函数(){
$(“#对话框登录消息”)。对话框({
莫代尔:是的,
按钮:{
Ok:函数(){
document.location.href=“/”;
$(此).dialog(“关闭”);
}
}
});
});

您未登录此网站。您需要登录此网站才能使用演示。;


如果用户未通过身份验证,则会显示此对话框。如果他们按OK,则会转到主页。但是,如果他们按escape键,对话框将通过,视图将继续可用。我想将用户发送回主页,但我不确定如何从这里开始。提前感谢。

您只需附加一个keyup/keydown事件并点击按钮即可

$(document).keyup(function(e) {
  if (e.keyCode === 27){ // 27 is esc keycode
     $(this).dialog("close");
     document.location.href = "/";                
  }
});
但是,此事件将绑定在页面上,因此随时按
esc
将重定向到主页

您要做的是添加一个
var modalState
,它将确定该对话框是否处于活动状态

<script>
   var modalState = 0; // declare it outside, 0 means unopened

   $(function () {
       modalState = 1; // change it to active

      $("#dialog-login-message").dialog({
         modal: true,
         buttons: {
            Ok: function () {
               document.location.href = "/";
               $(this).dialog("close");
            }
         }
      });
   });

   $(document).keyup(function(e) {
      if (e.keyCode === 27){ // 27 is esc keycode
         if(modalState == 1){ // check if dialog is active
            $(this).dialog("close");
            document.location.href = "/"; 
         }              
      }
   });
</script>

var modalState=0;//在外部声明,0表示未打开
$(函数(){
modalState=1;//将其更改为活动
$(“#对话框登录消息”)。对话框({
莫代尔:是的,
按钮:{
Ok:函数(){
document.location.href=“/”;
$(此).dialog(“关闭”);
}
}
});
});
$(文档).keyup(函数(e){
如果(e.keyCode===27){//27是esc键码
如果(modalState==1){//检查对话框是否处于活动状态
$(此).dialog(“关闭”);
document.location.href=“/”;
}              
}
});