使用javascript将用户类型转换为大写

But*_*her 37 javascript jquery keycode uppercase

当用户使用javascript键入时,我想将小写字母转换为大写.欢迎任何建议.

我尝试过以下方法:

$("#textbox").live('keypress', function (e) {
    if (e.which >= 97 && e.which <= 122) {
        var newKey = e.which - 32;
        // I have tried setting those
        e.keyCode = newKey;
        e.charCode = newKey;
    }
});
Run Code Online (Sandbox Code Playgroud)

ken*_*bec 56

css

#textbox{text-transform:uppercase}
Run Code Online (Sandbox Code Playgroud)

  • `text-transform:uppercase`也使输入框上的HTML 5`占位符`文本大写(我只在Chrome 41中测试过).不喜欢那样,所以我选择使用JavaScript解决方案. (4认同)
  • 它现在有效 - 第一个版本有`大写`而不是'大写'.:) +1来自我,因为这是一个聪明的答案,但实际上并没有改变文本框的价值. (3认同)

Sha*_*haz 23

$("#textbox").bind('keyup', function (e) {
    if (e.which >= 97 && e.which <= 122) {
        var newKey = e.which - 32;
        // I have tried setting those
        e.keyCode = newKey;
        e.charCode = newKey;
    }

    $("#textbox").val(($("#textbox").val()).toUpperCase());
});
Run Code Online (Sandbox Code Playgroud)

  • 光标位置丢失.阻止in if什么都没做. (3认同)

And*_* SK 9

这可能是一个很好的解决方法.只需在CSS中设置:

input#textbox {
    text-transform: uppercase;
}
Run Code Online (Sandbox Code Playgroud)

如果您将数据发布到服务器端脚本(假设是php),您可以始终执行以下操作:

$upper_data = strtoupper($_POST['textbox']);
Run Code Online (Sandbox Code Playgroud)


Yan*_*n86 5

嗨此代码适用于输入和textarea没有延迟或大写更改

$("#input").on('input', function(evt) {
              var input = $(this);
              var start = input[0].selectionStart;
              $(this).val(function (_, val) {
                return val.toUpperCase();
              });
              input[0].selectionStart = input[0].selectionEnd = start;
            });
Run Code Online (Sandbox Code Playgroud)

混合来自/sf/answers/556087591//sf/answers/920890841/

的jsfiddle