With jQuery, how do I capitalize the first letter of a text field while the user is still editing that field?

后端 未结 21 2500
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-27 16:26

I\'m looking for an example of how to capitalize the first letter of a string being entered into a text field. Normally, this is done on the entire field with a function, r

21条回答
  •  盖世英雄少女心
    2020-11-27 16:50

    $('input[type="text"]').keyup(function(evt){
        var txt = $(this).val();
    
    
        // Regex taken from php.js (http://phpjs.org/functions/ucwords:569)
        $(this).val(txt.replace(/^(.)|\s(.)/g, function($1){ return $1.toUpperCase( ); }));
    });
    

提交回复
热议问题