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

后端 未结 21 2351
爱一瞬间的悲伤
爱一瞬间的悲伤 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:45

    Just use CSS.

    .myclass 
    {
        text-transform:capitalize;
    }
    
    0 讨论(0)
  • 2020-11-27 16:46

    I use both CSS and jQuery solutions when achieving this. This will change both how it appears in the browser and the data value. A simple solution, that just works.

    CSS

    #field {
        text-transform: capitalize;
    }
    

    jQuery

    $('#field').keyup(function() {
        var caps = jQuery('#field').val(); 
        caps = caps.charAt(0).toUpperCase() + caps.slice(1);
        jQuery('#field').val(caps);
    });
    
    0 讨论(0)
  • 2020-11-27 16:46

    Jquery or Javascipt doesn't provide a built-in method to achieve this.

    CSS test transform (text-transform:capitalize;) doesn't really capitalize the string's data but shows a capitalized rendering on the screen.

    If you are looking for a more legit way of achieving this in the data level using plain vanillaJS, use this solution =>

    var capitalizeString = function (word) {    
        word = word.toLowerCase();
        if (word.indexOf(" ") != -1) { // passed param contains 1 + words
            word = word.replace(/\s/g, "--");
            var result = $.camelCase("-" + word);
            return result.replace(/-/g, " ");
        } else {
        return $.camelCase("-" + word);
        }
    }
    
    0 讨论(0)
  • 2020-11-27 16:47

    CSS solution with "text-transform: capitalize;" is no good if you want to use the contents of the input in backend. You will still receive data as-is. JavaScript solves this issue.

    JQuery plugin combined from some of the techniques mentioned earlier, plus it capitalizes words after hyphens, i.e.: "Tro Lo-Lo":

    Add to your script:

    jQuery.fn.capitalize = function() {
        $(this[0]).keyup(function(event) {
            var box = event.target;
            var txt = $(this).val();
            var stringStart = box.selectionStart;
            var stringEnd = box.selectionEnd;
            $(this).val(txt.replace(/^(.)|(\s|\-)(.)/g, function($word) {
                return $word.toUpperCase();
            }));
            box.setSelectionRange(stringStart , stringEnd);
        });
    
       return this;
    }
    

    Then just attach capitalize() to any selector:

    $('#myform input').capitalize();
    
    0 讨论(0)
  • 2020-11-27 16:47

    I used the code of @Spajus and wrote a more extended jQuery plugin.

    I wrote these four jQuery functions:

    • upperFirstAll() to capitalize ALL words in an inputfield
    • upperFirst() to capitalize only the FIRST word
    • upperCase() to convert the hole text to upper case
    • lowerCase() to convert the hole text to lower case

    You can use and chain them like any other jQuery function:
    $('#firstname').upperFirstAll()

    My complete jQuery plugin:

    (function ($) {
        $.fn.extend({
    
        // With every keystroke capitalize first letter of ALL words in the text
        upperFirstAll: function() {
            $(this).keyup(function(event) {
                var box = event.target;
                var txt = $(this).val();
                var start = box.selectionStart;
                var end = box.selectionEnd;
    
                $(this).val(txt.toLowerCase().replace(/^(.)|(\s|\-)(.)/g,
                function(c) {
                    return c.toUpperCase();
                }));
                box.setSelectionRange(start, end);
            });
            return this;
        },
    
        // With every keystroke capitalize first letter of the FIRST word in the text
        upperFirst: function() {
            $(this).keyup(function(event) {
                var box = event.target;
                var txt = $(this).val();
                var start = box.selectionStart;
                var end = box.selectionEnd;
    
                $(this).val(txt.toLowerCase().replace(/^(.)/g,
                function(c) {
                    return c.toUpperCase();
                }));
                box.setSelectionRange(start, end);
            });
            return this;
        },
    
        // Converts with every keystroke the hole text to lowercase
        lowerCase: function() {
            $(this).keyup(function(event) {
                var box = event.target;
                var txt = $(this).val();
                var start = box.selectionStart;
                var end = box.selectionEnd;
    
                $(this).val(txt.toLowerCase());
                box.setSelectionRange(start, end);
            });
            return this;
        },
    
        // Converts with every keystroke the hole text to uppercase
        upperCase: function() {
            $(this).keyup(function(event) {
                var box = event.target;
                var txt = $(this).val();
                var start = box.selectionStart;
                var end = box.selectionEnd;
    
                $(this).val(txt.toUpperCase());
                box.setSelectionRange(start, end);
            });
            return this;
        }
    
        });
    }(jQuery));
    

    Groetjes :)

    0 讨论(0)
  • 2020-11-27 16:48

    My personal favorite when using jQuery is short and sweet:

    function capitalize(word) {
       return $.camelCase("-" + word);
    }
    

    There's a jQuery plugin that does this too. I'll call it... jCap.js

    $.fn.extend($, { 
        capitalize: function() {
            return $.camelCase("-"+arguments[0]); 
        } 
    });
    
    0 讨论(0)
提交回复
热议问题