2

如何在文本框中降低大写字母、小写字母、删除、退格和禁用空格键?

 <html>
<head>
  <script type="text/javascript">
   function foo(e) {
   var allow=' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_'
   var k;
   k=document.all?parseInt(e.keyCode): parseInt(e.which);
   return (allow.indexOf(String.fromCharCode(k))!=-1);
   }
 </script>
 </head>
 <body>
   <input type="text" onKeyPress="return foo(event)" />
 </body>
 </html>

请帮帮我谢谢...

4

2 回答 2

1

尝试 Jquery Some What Like This 并继续努力。

$("[id*='yourid']").keydown(function (event) {
    // Allow: backspace, delete, tab, escape, and enter
    if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 ||
        // Allow: Ctrl+A
        (event.keyCode == 65 && event.ctrlKey === true) ||
        // Allow: home, end, left, right
        (event.keyCode >= 35 && event.keyCode <= 39)) {
        // let it happen, don't do anything
        return;
    } else {
        // Ensure that it is a number and stop the keypress
        if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105)) {
            event.preventDefault();
        }
    }
});
于 2013-10-01T11:37:59.907 回答
0

我想出了以下代码:

jQuery('#input_id', function(e){
    // Allow: backspace, delete, tab, escape, enter
    if (jQuery.inArray(e.keyCode, [46, 8, 9, 27, 13, 110]) !== -1 ||
        // Allow: Ctrl+A
        (e.keyCode === 65 && e.ctrlKey === true) ||
        // Allow: Ctrl+C
        (e.keyCode === 67 && e.ctrlKey === true) ||
        // Allow: Ctrl+X
        (e.keyCode === 88 && e.ctrlKey === true) ||
        // Bug in some Android devices where it is always 229
        (e.keyCode === 229) ||
        // Allow: home, end, left, right
        (e.keyCode >= 35 && e.keyCode <= 40)) {
        // let it happen, don't do anything
        return;
    }
    // Ensure that it is a number and stop the keypress
    if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
        e.preventDefault();
    }
});

另外表格应该有autocomplete="off"。如果没有此选项,您可能会遇到移动设备上的自动完成算法问题。

于 2015-06-19T12:29:34.333 回答