duanji1924 2011-08-27 20:09
浏览 75
已采纳

onkeydown事件阻塞除数字之外的所有输入,但允许输入提交表单?

I am having trouble with a form that displays results drawn out of a mysql database, based on a user supplied string.

An order form, so to say. And in this form there is a quantity field. I have tried to use javascript to block all input except for numeric values. Along with this though, I have disabled the enter key functionality.

I know that this keycode is 23. But how do I allow a user may press enter to submit the form, working with the following peice of code:

onkeydown="return ( event.ctrlKey || event.altKey 
                || (47<event.keyCode && event.keyCode<58 && event.shiftKey==false) 
                || (95<event.keyCode && event.keyCode<106)
                || (event.keyCode==8) || (event.keyCode==9) 
                || (event.keyCode>34 && event.keyCode<40) 
                || (event.keyCode==46) )"

Any input with this would be greatly appreciated, thanks.

  • 写回答

1条回答 默认 最新

  • doufu1970 2011-08-27 20:22
    关注

    I think this might help you:

    <!DOCTYPE html>
    <html>
        <head>
            <title></title>
            <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
            <script type="text/javascript">
                $(function() {
                    $('#numeric').keydown(function(e) {
                        return ((e.keyCode >= 48 && e.keyCode <= 57) || e.keyCode == 13)
                    });
                });
            </script>
        </head>
    
        <body>
            <form method="post" action="">
                <input type="text" id="numeric" />
            </form>
        </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?