JavaScript 关键代码

发布于 2024-11-17 05:58:05 字数 922 浏览 1 评论 0原文

我正在使用一个不是我编写的 JavaScript 例程。它是从文本框的 onkeydown 属性调用的,以防止不必要的击键。

第一个参数显然没有被使用。第二个参数是应该允许的字符列表。

function RestrictChars(evt, chars) {
    var key;
    var keychar;

    if (window.event)
        key = window.event.keyCode;
    else if (e)
        key = e.which;
    else
        return true;

    keychar = String.fromCharCode(key);

    if ((key == null) || (key == 0) || (key == 8) ||
        (key == 9) || (key == 13) || (key == 27))
        // Control key
        return true;
    else if (((chars).indexOf(keychar) > -1))
        return true;
    else
        return false;
}

这似乎适用于字母数字字符。但是,诸如 ./ 之类的字符会导致此函数返回 false,即使这些字符包含在 chars参数。例如,如果按下 . 键,key 将设置为 190,并且 keychar 将设置为“3/4”字符。

谁能明白这是如何工作的和/或为什么不起作用?我对 JavaScript 的了解还不够,无法了解它想要做什么。

I'm working with a JavaScript routine I didn't write. It is called from a text box's onkeydown attribute to prevent unwanted keystrokes.

The first argument is apparently not used. The second argument is a list of characters that should be allowed.

function RestrictChars(evt, chars) {
    var key;
    var keychar;

    if (window.event)
        key = window.event.keyCode;
    else if (e)
        key = e.which;
    else
        return true;

    keychar = String.fromCharCode(key);

    if ((key == null) || (key == 0) || (key == 8) ||
        (key == 9) || (key == 13) || (key == 27))
        // Control key
        return true;
    else if (((chars).indexOf(keychar) > -1))
        return true;
    else
        return false;
}

This seems to work for alpha-numeric characters. However, characters such as . and / cause this function to return false, even when these characters are included in the chars parameter. For example, if the . key is pressed, key is set to 190, and keychar gets set to the "3/4" character.

Can anyone see how this was meant to work and/or why it doesn't? I don't know enough about JavaScript to see what it's trying to do.

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

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

发布评论

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

评论(2

浴红衣 2024-11-24 05:58:05

有两个问题:首先,如果您正在分析输入的字符,则需要使用 keypress 事件而不是 keydown 因为这是唯一的事件它告诉您有关实际键入的字符的任何可靠信息。有关此事件和一般 JavaScript 键事件的(很多)更多详细信息,请参阅 http://unixpapa.com /js/key.html。其次,引用了一个名为 e 的变量,该变量不(但应该)与 evt 参数对应。

这是一个重写,假设您有一个名为 textBox 的变量,它引用文本输入元素。

jsFiddle: http://jsfiddle.net/9DZwL/

代码:

function isKeypressCharValid(e, chars) {
    e = e || window.event;

    // Allow delete, tab, enter and escape keys through
    if (/^(8|9|13|27)$/.test("" + e.keyCode)) {
        return true;
    }

    var charCode = (typeof e.which == "number") ? e.which : e.keyCode;
    var charTyped = String.fromCharCode(charCode);
    return chars.indexOf(charTyped) > -1;
}

textBox.onkeypress = function(evt) {
    if (!isKeypressCharValid(evt, "abc123")) {
        return false;
    }
};

Two things are wrong with that: first, if you're analysing what character has been typed, you need to use the keypress event instead of keydown because that is the only event that tells you anything reliable about the actual character typed. For (a lot) more detail about about this and JavaScript key events in general, see http://unixpapa.com/js/key.html. Second, there are references to a variable called e which doesn't (but should) correspond with the evt parameter.

Here's a rewrite, assuming you have a variable called textBox that refers to the text input element.

jsFiddle: http://jsfiddle.net/9DZwL/

Code:

function isKeypressCharValid(e, chars) {
    e = e || window.event;

    // Allow delete, tab, enter and escape keys through
    if (/^(8|9|13|27)$/.test("" + e.keyCode)) {
        return true;
    }

    var charCode = (typeof e.which == "number") ? e.which : e.keyCode;
    var charTyped = String.fromCharCode(charCode);
    return chars.indexOf(charTyped) > -1;
}

textBox.onkeypress = function(evt) {
    if (!isKeypressCharValid(evt, "abc123")) {
        return false;
    }
};
对风讲故事 2024-11-24 05:58:05

我也不是 JS 人,但是...我可以解释它应该如何工作;然而,我不知道为什么你会得到你提到的键的值。

keychar = String.fromCharCode(key);

这会检查该键是否为可打印字符(字母、标点符号等)。

if ((key == null) || (key == 0) || (key == 8) ||
    (key == 9) || (key == 13) || (key == 27))
    // Control key

上面的检查会检查该键是否为 null OR (||)` 0 或 8(退格键)或9(制表符)或 13(0x0D 或 ENTER)或 27(0x1B 或 ESCAPE)- 这正是您期望的布尔结果: IF或 <那个条件>或<另一条件>或...

else if (((chars).indexOf(keychar) > -1))

这会检查 keychar 是否在作为 chars 参数传递的字符串中

I'm not a JS person, either, but... I can explain how it's supposed to work; I don't know why you're getting the values you are for the keys you mentioned, however.

keychar = String.fromCharCode(key);

This checks to see if the key is a printable character (letter, punctuation mark, etc.)

if ((key == null) || (key == 0) || (key == 8) ||
    (key == 9) || (key == 13) || (key == 27))
    // Control key

The above checks to see if the key is null OR (||)` 0 or 8 (backspace) or 9 (tab) or 13 (0x0D, or ENTER) or 27 (0x1B or ESCAPE) - it's exactly the Boolean result you'd expect: IF <thiscondition> or <thatcondition> or <anothercondition> or ...

else if (((chars).indexOf(keychar) > -1))

This checks to see if the keychar is in the string of characters passed as the chars parameter

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