Capitalize first character of user input in real time with JQuery

后端 未结 8 1263
-上瘾入骨i
-上瘾入骨i 2021-02-14 13:15

I am trying to auto-capitalize the first character of a textarea/input that the user inputs. The first attempt looked like this:

$(document).ready(function() {
         


        
8条回答
  •  面向向阳花
    2021-02-14 14:04

    Try this:

    $(document).ready(function() {
        $('input').on('keydown', function(e) {
            if (this.value == '') {
            var char = String.fromCharCode(e.which);
                if (char.match(/^\w$/)) {
                    // If is empty and we pressed a printable key...
                    this.value = char.toUpperCase();
                    return false;
                }
            }
        });
    });
    

    See in use here.

提交回复
热议问题