1 回答

TA貢獻1841條經驗 獲得超3個贊
您可以簡單地附加一個 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>
添加回答
舉報