Keydown事件代码

时间:2009-09-15 13:57:43

标签: jquery

在文本框的KeyDown事件中,我可以检查一系列keyCode

例如:

if (e.keyCode == 90 to 97 || e.keyCode == 104 to 110)

如何以正确的方式写出来?

4 个答案:

答案 0 :(得分:6)

var inInterval = function (code, min, max) {
    return code >= min && code <= max;
};

if (inInterval(e.keyCode, 90, 97) || inInterval(e.keyCode, 104, 110))

或者,更好一点:

var interval = function (min, max) {
    return {
        min: min,
        max: max,
        contains : function (elem) {
            return this.min <= elem && elem <= this.max;
        }
    };
};

interval(90, 98).contains(92); // true
interval(90, 98).contains(15); // false

答案 1 :(得分:1)

if ((e.keyCode > 89 && e.keyCode < 98) || (e.keyCode > 103 && e.keyCode < 111))

或:

if ((e.keyCode >= 90 && e.keyCode <= 97) || (e.keyCode >= 104 && e.keyCode <= 110))

答案 2 :(得分:0)

if ((90 <= e.keyCode && e.keyCode <= 97) || (104 <= e.keyCode && e.keyCode <= 110))

答案 3 :(得分:0)

var myKeycodes = [1,2,3,4,5,6];
if(myKeycodes.indexOf(e.keyCode) != -1){
    // your keycode is in the array, do stuff
}