当前位置: 动力学知识库 > 问答 > 编程问答 >

keydown - JQuery Preventing Multiple Back Quotes on Input Box

问题描述:

I have the following code that only allows a user to enter any number from the keyboard

or Num Pad and single decimal point. It's works fairly well but I discovered that

it doesn't prevent multiple back quotes (see ` button under the Esc key) from being entered. Any ideas why the following doesn't work for KeyCode = 223? I call the following function using a keydown() Event and using i'm JQuery 1.10.2, Crome and Firefox.

var keyCode = event.which || event.keyCode;

var isShift = event.shiftKey ? true : false;

if(isShift){

switch(keyCode){

case 16:

break;

default:

//Allow user to use Shift and End 35, Home 36, Left Arrow 37, Right Arrow 39, Tab 9.

if(keyCode!= 35 && keyCode!= 36 && keyCode!= 37 && keyCode!= 39 && keyCode!= 9) {

event.preventDefault();

}

}

//Allow delete 46, Tab 9, Backspace 8, Shift 16, End 35, Home 36, Left Arrow 37, Right Arrow 39.

}else if(keyCode != 46 && keyCode != 9 && keyCode != 8 && keyCode != 16 && keyCode != 35 && keyCode != 36 && keyCode != 37 && keyCode != 39){

if(((keyCode != 190 && keyCode != 110) || $(inputBox).val().indexOf('.') != -1) && (keyCode < 48 || keyCode > 57 && keyCode != 96 && keyCode != 97 && keyCode != 98 && keyCode != 99 && keyCode != 100 && keyCode != 101 && keyCode != 102 && keyCode != 103 && keyCode != 104 && keyCode != 105)) {

event.preventDefault();

}

}

网友答案:

I think the reason why it is not detecting the backtick is your keyboard settings, I just tested this and can confirm, that changing your keyboard settings to English (United Kingdom) and making it the default keyboard language, I changed from English (Irish) which would not stop the backtick from been entered. When changed to English (United Kingdom) it would not allow me to input the backtick.

  1. Windows 7
  2. Goto -> Control Panel
  3. Clock, Region & Language
  4. Change keyboards or other input methods
  5. Keyboard & Languages Tab
  6. Change Keyboards
  7. Add -> English (United Kingdom) -> Keyboard -> United Kingdom
  8. Click Apply
  9. Set it as your default keyboard language
  10. Go to Language Bar -> Select United Kingdom
  11. Test j809's fiddle again.

So the keyCodes are actually determined by the keyboard language, makes sense the classic example of US keyboards @ symbol is SHIFT + 2, UK @ symbol is SHIFT + (Key to left of Hash Key)

Video Here

分享给朋友:
您可能感兴趣的文章:
随机阅读: