开发者

Firebug: "The 'charCode' property of a keyup event should not be used. The value is meaningless."

I'm building a calculator, and am using the following:

jQuery(function($) {
    $('#Quantity').keyup(function() {
        console.log($(this).val());
    });
});

Q1: Is keyup() the right event to be using?

Q2: Why is Firebug telling me "The 'charCode' property of a ke开发者_开发技巧yup event should not be used. The value is meaningless"? I noticed that it's doing the same thing as I type this question into Stackoverflow, so I must not be doing something too awfully wrong.


It appears because jQuery uses its own event object instead of a native browser one, and in the process of creating this object reads every possible property of an event, including charCode:

props = [ ... "charCode" ... ]

for ( var i = this.props.length, prop; i; ) {
    prop = this.props[ --i ];
    event[ prop ] = originalEvent[ prop ];
}

Firefox rightly warns you that you don't want to read charCode in the case of keyup events, not realising that you're just copying it, not actually doing anything with it. Consequently any page you create with jQuery keyup/keydown handlers will spit endless warnings.

(IMO, jQuery should not bother copy this property. In the only case you actually want to use it—keypress—you can just use plain old which.)


1) Yes, you are using the right event with keyup, so you get the latest value.

2) You can safely ignore this Firebug warning, it appears in many, many applications and is a result of how jQuery normalizes the .which property for events, firebug should be a bit smarter about this IMO.


This is not directly answering your question, but might provide useful information.

From quirksmode.org:

keyCode and charCode

The two properties are keyCode and charCode. Put (too) simply, keyCode says something about the actual keyboard key the user pressed, while charCode gives the ASCII value of the resulting character. These bits of information need not be the same; for instance, a lower case 'a' and an upper case 'A' have the same keyCode, because the user presses the same key, but a different charCode because the resulting character is different.

Explorer and Opera do not support charCode. However, they give the character information in keyCode, but only onkeypress. Onkeydown and -up keyCode contains key information.

I can only assume that the charCode value is "meaningless" if a key is pressed that does not print a character (like arrow keys). Also, the charCode might be different from what you expect if the user uses a non-English keyboard. But I am not sure about this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜