javascript (jquery) numeric input: keyCode for '3' and '#' are the same

前端 未结 4 1974
面向向阳花
面向向阳花 2021-01-17 23:33

I need to set up an so that it will accept only numeric chars, backspace, delete, enter, tabs and arrows.

There\'s a lot o

4条回答
  •  -上瘾入骨i
    2021-01-18 00:12

    jQuery also provides a shiftkey boolean on the event object:

    $('#myTextBox').keydown(function(e){
      if(e.keyCode === 51 && !e.shiftKey){
         // '3' key pressed while shift was **not** held down (not '#')
      }
    });
    

    EDIT I reread your question and changed the code above for !shiftkey

提交回复
热议问题