本教程操作环境:windows10系统、javascript1.8.5版、dell g3电脑。
javascript怎样设置文本框不能输入数字
数字0-9keycode的值为48-57,只要在javascript中设置文本框中输入keycode的值在这个范围内就就取消此行为,以此就可以实现文本框不能输入数字。
示例如下:
或者:
<!doctype html><html lang="en"><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <title>document</title></head><body><input type="text" id="test"> <script type="text/javascript"> window.onload=function(){ document.getelementbyid('test').addeventlistener('keypress',function(e){ var charcode=e.charcode; if(charcode>46&&charcode<58) /*0-9 的charcode*/ e.preventdefault(); }); } </script></body></html>
输出结果与上述结果相同,文本框内无法输入数字。
【相关推荐:javascript学习教程】
以上就是javascript怎样设置文本框不能输入数字的详细内容。
