admin管理员组

文章数量:1327685

I have code similar to this:

$("#some-input").keyup(function (event) {
  var availableKeys = [".", ",", ";"];
  var key = String.fromCharCode(event.which);
  if (availableKeys.indexOf(key) != -1) {
    alert("Derp");
  }
});

However it does not work as I expected. The event.which/fromCharCode bination works like this properly on digits or letters (capital ones) and this is the correct behaviour since (I presume) event keycodes are different than charset entries.

Is there a workaround other than directly specifying keycodes for parison (keeping the array of characters is a priority)?

I have code similar to this:

$("#some-input").keyup(function (event) {
  var availableKeys = [".", ",", ";"];
  var key = String.fromCharCode(event.which);
  if (availableKeys.indexOf(key) != -1) {
    alert("Derp");
  }
});

However it does not work as I expected. The event.which/fromCharCode bination works like this properly on digits or letters (capital ones) and this is the correct behaviour since (I presume) event keycodes are different than charset entries.

Is there a workaround other than directly specifying keycodes for parison (keeping the array of characters is a priority)?

Share asked Dec 14, 2011 at 9:44 PrzemekPrzemek 6,71013 gold badges46 silver badges64 bronze badges 3
  • event.which is the best option because it normalizes event.keyCode and event.charCode – Przemek Commented Dec 14, 2011 at 9:53
  • do you have problems about buttons like backspace ? – kommradHomer Commented Dec 14, 2011 at 9:58
  • please check my answer, you will get some more information. – dku.rajkumar Commented Dec 14, 2011 at 10:18
Add a ment  | 

6 Answers 6

Reset to default 4

Try changing your array to contain the keycodes of the keys you wish to check for:

$("#some-input").keyup(function(event) {
    var availableKeys = [190, 188, 59]; // ".", ",", ";"
    if (availableKeys.indexOf(event.which) != -1) {
        alert("Derp");
    }
});

Example fiddle here

Here's a full list of keycodes

Edit
After a little investigation it appears your method should work, but the implementation of String.fromCharCode is flawed - at least for the symbol keys. While pressing . returns the correct keyCode of 190, when you run that through fromCharCode you get the string ¾. Other keys such as [ and # show similar behaviour. I assume this is due to internationalisation, maybe someone else could confirm.

You could use keypress instead. Like

$("#some-input").bind('keypress', function(e) {
    var availableKeys = [".", ",", ";"];

    if(availableKeys.indexOf( String.fromCharCode( e.which ) ) > -1 ) {
        alert('Derp');
    }
});

Looks to me as though you should try using charCodeAt to convert your keys to keycodes at parison time. eg:

alert (".".charCodeAt(0)); // alerts 46

https://developer.mozilla/en/JavaScript/Reference/Global_Objects/String/charCodeAt

This very likely is an issue related to the way you are getting the key/char code. Try getting the char/key code like:

var code = (event.charCode != 0) ? event.charCode : (event.keyCode != 0 ? event.keyCode : event.which);
var key = String.fromCharCode(code);

Can't find any issues in any other part of your code.

This is not working because you are using ' as one of the key and also as char separator in the array so its confusing the program otherwise its working for . and ;. ["." , " , " , ";"] . It consider 4 different elements. use belwo code:

$("#some-input").keypress(function (event) {
 var availableKeys = ",.;";
  var key = String.fromCharCode(event.which);
  if (availableKeys.indexOf(key) != -1) {
    alert("Derp");
  }
});

fiddle : http://jsfiddle/AVcrw/1/

You could use underscore.js:

$("#some-input").bind('keypress', function(event) {
        var aKeys = [13, 32];

        if ( _.contains(aKeys, event.which) ) {
            console.log("char :"  + event.which);
        }
});

本文标签: javascriptDetermine if key pressed is in provided arrayStack Overflow