JavaScript 关键代码
我正在使用一个不是我编写的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有两个问题:首先,如果您正在分析输入的字符,则需要使用
keypress
事件而不是keydown
因为这是唯一的事件它告诉您有关实际键入的字符的任何可靠信息。有关此事件和一般 JavaScript 键事件的(很多)更多详细信息,请参阅 http://unixpapa.com /js/key.html。其次,引用了一个名为e
的变量,该变量不(但应该)与evt
参数对应。这是一个重写,假设您有一个名为
textBox
的变量,它引用文本输入元素。jsFiddle: http://jsfiddle.net/9DZwL/
代码:
Two things are wrong with that: first, if you're analysing what character has been typed, you need to use the
keypress
event instead ofkeydown
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 callede
which doesn't (but should) correspond with theevt
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:
我也不是 JS 人,但是...我可以解释它应该如何工作;然而,我不知道为什么你会得到你提到的键的值。
这会检查该键是否为可打印字符(字母、标点符号等)。
上面的检查会检查该键是否为 null OR (或 <那个条件>或<另一条件>或...
||
)` 0 或 8(退格键)或9(制表符)或 13(0x0D 或 ENTER)或 27(0x1B 或 ESCAPE)- 这正是您期望的布尔结果: IF这会检查
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.
This checks to see if the key is a printable character (letter, punctuation mark, etc.)
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 ...This checks to see if the
keychar
is in the string of characters passed as thechars
parameter