键入时转换为大写,有一些延迟

Ram*_*amy 10 javascript css jquery

我在输入时使用以下代码转换为大写.

      $(".input_capital").live('keypress', function(e)
      {
        var defaultStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        var checkstr = $(this).val();
        var str1 = '';
        for (i = 0; i < checkstr.length; i++)
        {
            var ch = checkstr.charCodeAt(i);
             if (ch>=97 && ch<=122){
                str1 += defaultStr.charAt(ch-97);
             }else{
                str1 += checkstr.charAt(i);
              }
        }
        $(this).focus();
        $(this).val(str1);
     });
Run Code Online (Sandbox Code Playgroud)

以下代码

  $(".input_capital").live('keypress', function(e)
  {
       $(this).val($(this).val().toUpperCase());
  });
Run Code Online (Sandbox Code Playgroud)

以上所有代码都运行正常.但对于能够查看较低情况一段时间的用户.之后只有它转换为大写.

text-transform: uppercase在css中试过' '.但它不适用于Android Os的三星标签.请任何人帮助我通过脚本实现这一目标.

Yos*_*shi 22

你可以试试:

$(".input_capital").on('keydown', function(evt) {
  $(this).val(function (_, val) {
    return val + String.fromCharCode(evt.which).toUpperCase();
  });

  return false;
});?
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/5gLyX/

它有一些缺陷,但我认为这个想法是明确的,并且可以建立在其上.


更好的版本,但要注意inputIE <9中不支持的事件.

$('.input_capital').on('input', function(evt) {
  $(this).val(function(_, val) {
    return val.toUpperCase();
  });
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea class='input_capital' rows='8'></textarea>
Run Code Online (Sandbox Code Playgroud)


Som*_*Guy 21

Note: This answer is without delay..if you like to avoid Jquery

尝试

<input type="text" style="text-transform: uppercase">
Run Code Online (Sandbox Code Playgroud)

演示 - JSFiddle

  • 这只给出_appearance_表示大写/小写的文本.如果您将值发送到服务器,它将是大小写混合. (15认同)
  • 最好的答案IMO (11认同)

gui*_*cao 5

如果您不在乎 ie<9 ,请听“输入”事件

$(".input_capital").bind("input", function(e){
   this.value = this.value.toUpperCase();
});
Run Code Online (Sandbox Code Playgroud)

对于 IE<9,请改用“keyup”

演示:http : //jsfiddle.net/MupXZ/6/