jquery + ie8:未检测到退格键?

发布于 2024-10-06 14:55:56 字数 281 浏览 0 评论 0原文

我想在我的输入字段之一中禁用退格键,因此我编写了以下 jquery:

$("#noBackspacesHere").keypress(function(e){
            if(e.which == "8"){
                return false;
            }
        });

这在 Firefox 中工作正常,但在 ie8 中似乎不起作用。 我已经读到 .keyCode 有问题,所以这就是我选择 .which 的原因。

I want to disable backspace in one of my inputfields so I wrote the following jquery:

$("#noBackspacesHere").keypress(function(e){
            if(e.which == "8"){
                return false;
            }
        });

This works fine in firefox, but doesn't seem to work in ie8.
I've already read that .keyCode has issues so that's why I went for .which.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

难理解 2024-10-13 14:55:56

@Jonathon Bolster 的代码不适用于 Opera。

这应该适用于 Opera、IE、FF 和 Chrome。

$("#noBackspacesHere").bind("keypress keydown", function(e) {
    if (e.which == 8) {
        e.preventDefault();
    }
});

此外,e.which 是检查关键代码的首选方法,因为 jQuery 规范了事件对象,因此无论您使用哪种浏览器,which 都将始终存在。

@Jonathon Bolster's code doesn't work for Opera.

This should work in Opera, IE, FF and Chrome.

$("#noBackspacesHere").bind("keypress keydown", function(e) {
    if (e.which == 8) {
        e.preventDefault();
    }
});

Also, e.which is the preferred way to check for key codes because jQuery normalizes the event object so that which will always be present no matter what browser you use.

白昼 2024-10-13 14:55:56

我刚刚玩弄了键盘事件, keydown 会在这里帮助您:

$("#noBackspacesHere").keydown(function(e) {
    if (e.which == 8) {
        return false;
    }
});

示例: http: //jsfiddle.net/jonathon/7xBRf/

我做了 typeof(e.which) ,它说它是一个数字,所以你不需要它周围的引号。我已经在 IE8、Chrome 和 FF 中对此进行了测试,它似乎对它们有效。

I just played around with the keyboard events and keydown will help you here:

$("#noBackspacesHere").keydown(function(e) {
    if (e.which == 8) {
        return false;
    }
});

Example: http://jsfiddle.net/jonathon/7xBRf/

I did typeof(e.which) and it said that it's a number, so you don't need the quotes around it. I've tested this in IE8, Chrome and FF and it seems to work for them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文