防止退格从当前表单返回 [英] Preventing backspace to go back from the current form

查看:108
本文介绍了防止退格从当前表单返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面的JavaScript函数来防止回退。

  function preventBackspace(e){
var evt = e || window.event;
if(evt){
var keyCode = evt.charCode || evt.keyCode;
if(keyCode === 8){
if(evt.preventDefault){
evt.preventDefault();
} else {
evt.returnValue = false;
}
}
}
}

I已将 onkeydown =preventBackspace();添加到所有文本框中。

我有两个单选按钮和两个文本框,这些文本框在选中时使其他文本框只读。当点击退格键时,它不会返回页面,但我无法从可编辑文本框中删除。请建议。当用户目前专注于一个文本框时,退格键并不是因为原因造成的原因 浏览器回去。文本框中的退格符用于删除一个字符 - 并且由于您已经添加了 preventDefault(),所以您可以阻止该行为发生。



如果您的目标是防止用户在完成表单之前意外离开表单,您可以使用 window.onbeforeunload 显示警告允许用户取消导航的消息:

  window.onbeforeunload = function(){
return你确定你想离开这个页面?你目前的条目+
将会丢失。;
};


I'm using the below JavaScript function to prevent backspace from going back.

function preventBackspace(e) {
    var evt = e || window.event;
    if (evt) {
        var keyCode = evt.charCode || evt.keyCode;
        if (keyCode === 8) {
            if (evt.preventDefault) {
                evt.preventDefault();
            } else {
                evt.returnValue = false;
            }
        }
    }
}

I have added onkeydown="preventBackspace();" to all the text boxes.

I have two radio buttons and two textboxes which when checked make the other textbox readonly. When hitting the backspace key it is not going to back page, but I am not able to delete from the editable text box. Please suggest. Thanks!

解决方案

When the user is currently focused on a textbox, the backspace key does not cause the browser to go back. The backspace in a textbox is used to delete a character - and since you've added preventDefault(), you're stopping that behavior from happening.

If your goal is to prevent the user from accidentally leaving the form before they are finished, you can use window.onbeforeunload to display a warning message that allows the user to cancel navigation:

window.onbeforeunload = function() {
    return "Are you sure you want to leave this page?  Your current entries" +
        " will be lost.";
};

这篇关于防止退格从当前表单返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆