Shiftkey是否在JavaScript中保持不变 [英] Is the shiftkey held down in JavaScript

查看:85
本文介绍了Shiftkey是否在JavaScript中保持不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个只允许输入数字的JS函数。该函数的副本如下:

I have written a JS function that only allow numbers to be entered. A copy of that function is below:

function NumbersOnly(e) {
    var evt = e || window.event;
    if (evt) {
        var keyCode = evt.charCode || evt.keyCode;
        //Allow tab, backspace and numbers to be pressed, otherwise return false for everything.
        //(keyCode>=96 && keyCode<=105) are the numpad numbers        
        if ((keyCode >= 48 && keyCode <= 57) || (keyCode >= 96 && keyCode <= 105) || keyCode === 9 || keyCode === 8) {


        }
        else {

            evt.returnValue = false;
        }
    }
}

此功能适用于所有数字,但我的问题发生在按住Shift键并按下其中一个数字键时。返回的值是数字上方的字符之一。因此,例如,如果我按住shift并按7​​,则返回'&'但keyCode仍然是55!我原本预计会有所不同。

This function works fine with all the numbers but my problem happens when the shift key is held down and one of the number keys is pressed. The value returned is one of the characters above the numbers. So for example if I hold down shift and press 7, '&' is returned but the keyCode is still 55!! I would have expected that to be different.

所以我的问题是如何检查换档键是否被按下。
我尝试过以下检查,但这不起作用:

So my question is how do I check if the shift key is being held down. I've tried the following check but this didn't work:

    if (keyCode === 16) {
        evt.returnValue = false;
    }
    else {

        if ((keyCode >= 48 && keyCode <= 57) || (keyCode >= 96 && keyCode <= 105) || keyCode === 9 || keyCode === 8) {


        }
        else {

            evt.returnValue = false;
        }
    }

我正在使用ASP.NET 4.0。

I'm using ASP.NET 4.0.

我们将非常感谢您的帮助。

Any help would be gratefully received.

提前致谢。

推荐答案

您可以使用以下方式检查是否按下了Shift键:

You can check if shift key is pressed using :

if(evt.shiftKey) {
 ...  //returns true if shift key is pressed

这篇关于Shiftkey是否在JavaScript中保持不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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