Convert to uppercase as user types using javascript

后端 未结 12 1556
清酒与你
清酒与你 2020-12-13 17:59

I want to convert lowercase chars to uppercase as the user types using javascript. Any suggestions are welcome.

I have tried the following:

$(\"#text         


        
12条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-13 18:34

    Hi this code works fine on input and textarea without delay or capitalization change

    $("#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;
                });
    

    mix from https://stackoverflow.com/a/7944108/1272540 and https://stackoverflow.com/a/13155583/1272540

    jsFiddle

提交回复
热议问题