js控制输入框只能输入数字不能输入其他字符

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">

    </style>
    <script src="../jquery/jquery-3.3.1.min.js"></script>
    <script type="text/javascript">
        function test(event){
            var test = document.getElementById("test");
            if(event.keyCode<48 || event.keyCode>57){  //判断keyCode码
                return false;
            }
        }
    </script>
</head>
<body>

<input type="text" onkeypress="return test(event)" id="test">
</body>
</html>

 

posted on 2018-11-21 19:10  xxcxxc  阅读(1494)  评论(0编辑  收藏  举报