且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

防止按下文本框中的键

更新时间:2023-12-02 22:52:58

你不能阻止软件按任何键,相当显然。 :-)



您可以通过忽略某些点击来进行控制。例如,这是如何过滤掉除数字之外的所有字符:



You cannot prevent pressing any key by the software, quite apparently. :-)

You can make some control ignoring some clicks. For example, this is how to filter out all characters but digits:

<html>
   <head>
      <script type="text/javascript"><!--
         function filterDigits(eventInstance) { 
            eventInstance = eventInstance || window.event;
                key = eventInstance.keyCode || eventInstance.which;
            if ((47 < key) && (key < 58) || key = 45 || key == 8) {
               return true;
            } else {
               if (eventInstance.preventDefault)
                  eventInstance.preventDefault();
               eventInstance.returnValue = false;
               return false;
            } //if
         } //filterDigits
      --></script>
   </head>
<body>

<input type="text" onkeypress="filterDigits(event)"/>

</body>
</html>





注意你还必须允许#8(退格)。



-SA


在这种情况下,谢谢你的解决方案是你需要的,但是在这里看看这个JQuery插件http://code.google.com/p/jquery-ui-plugins/wiki/TextInput [ ^ ]因为它可能有用供将来参考。



如果您需要关于按键处理的更多详细信息,请链接h以及更多实施细节: [ ^ ]。
Sergey's solution is what you need in this case, but take also a look at this JQuery plugin here http://code.google.com/p/jquery-ui-plugins/wiki/TextInput[^] as it could be usefull for future references.

If you need more details on keypresses processing, this link has also some more implementation details: [^].