如何忽略文本框中不需要的字符(JavaScript或jQuery)

Jer*_*NET 4 javascript jquery

有一个TextBox,我想让用户只输入数字而不是任何字母字符.那么我怎么能忽略最终用户通过JavaScript或jQuery输入的那些字符呢?注意我不想用空字符串替换用户输入的值; 相反,如果有任何办法可以忽略案例.

小智 6

尝试该代码:

  $("#id").keypress(function (e)
     {
     //if the letter is not digit then display error and don't type anything
     if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57))
     {
     return false;
     }
     });
Run Code Online (Sandbox Code Playgroud)

参考" http://roshanbh.com.np/2008/04/textbox-accept-only-numbers-digits.html "