6

我正在使用流行的 Firefox 扩展Greasemonkey

我想知道是否有办法以某种形式将所有文本输入大写,所以如果我使用 jQuery,代码将如下所示:

$('form#formid input[type=text]').capitalize();

现在我当然知道.capitalize()这不是一个有效的函数,为了将文本大写,你需要一个复杂的 JavaScript 代码,但在谷歌搜索之后,我找不到一个似乎可以在 Greasemonkey 中实现的函数。

有人可以帮我写这个脚本吗?

通过大写,我的意思是大写每个单词的第一个字母,比如 CSS text-transform:capitalize;,它必须覆盖用户可能输入的字母,也许在表单提交时这样做会更容易......

谢谢。

4

6 回答 6

10
//add a function to jQuery so we can call it on our jQuery collections
$.fn.capitalize = function () {

    //iterate through each of the elements passed in, `$.each()` is faster than `.each()
    $.each(this, function () {

        //split the value of this input by the spaces
        var split = this.value.split(' ');

        //iterate through each of the "words" and capitalize them
        for (var i = 0, len = split.length; i < len; i++) {
            split[i] = split[i].charAt(0).toUpperCase() + split[i].slice(1);
        }

        //re-join the string and set the value of the element
        this.value = split.join(' ');
    });
    return this;
};

这是一个演示:http: //jsfiddle.net/jasper/qppGQ/1/

这可以在事件处理程序中使用,以始终保持大写的文本正文:

//when the user presses a key and the value of the `textarea` is changed, the new value will have all capitalized words
$('textarea').on('keyup', function () {
    $(this).capitalize();
}).capitalize();//also capitalize the `textarea` element(s) on initialization

这是一个演示:http: //jsfiddle.net/jasper/qppGQ/2/

更新

要将第一个字母大写而单词的其余部分小写,我们可以.toLowerCase()在第一个字母大写后在字符串的其余部分中使用:

        ...
        for (var i = 0, len = split.length; i < len; i++) {
            split[i] = split[i].charAt(0).toUpperCase() + split[i].slice(1).toLowerCase();
        }
        ...

这是一个演示:http: //jsfiddle.net/jasper/qppGQ/3/

于 2012-03-06T01:20:23.110 回答
4

这是你想要做的吗?

(function ($) {

    $.fn.capitalize = function () {
        return this.val(function (i, oldValue) {
            return oldValue.charAt(0).toUpperCase() + oldValue.slice(1);
        });
    };

})(jQuery);

如果您想“实时”执行此操作,您可以在事件内部处理该操作:

(function ($) {

    $.fn.capitalize = function () {
        this.on('keyup.capitalize change.capitalize', function () { 
            $(this).val(function (i, oldValue) {
                return oldValue.charAt(0).toUpperCase() + oldValue.slice(1);
            });
        });
    };

})(jQuery);
于 2012-03-06T01:15:45.337 回答
1

我对大写函数的简单实现用空格分割字符串,并将每个单词的第一个字母大写。它假设每个单词至少被一个空格分隔。

function capitalize(text) {
    var i, words, w, result = '';

    words = text.split(' ');

    for (i = 0; i < words.length; i += 1) {
        w = words[i];
        result += w.substr(0,1).toUpperCase() + w.substr(1);
        if (i < words.length - 1) {
            result += ' ';    // Add the spaces back in after splitting
        }
    }

    return result;
}

控制台输出示例:

> capitalize("some tEsT e strING a B c 1 2 3")
  "Some TEsT E StrING A B C 1 2 3"
于 2012-03-06T01:19:53.060 回答
1

我在这里得到了提示,并使用标准 javascript 制定了一些更容易的方法。我需要大写 iPad Web App 上输入框的每个单词,自动大写无法始终如一地工作。这是解决方法:

HTML 输入

<input type="text" id="this_input" name="this_input" onkeyup="javascript:capitalize(this.id, this.value);">

Javascript

<script type="text/javascript">
  function capitalize(textboxid, text) {
      // string with alteast one character
      var i, words, w, result = '';

    words = text.split(' ');

    for (i = 0; i < words.length; i += 1) {
        w = words[i];
        result += w.substr(0,1).toUpperCase() + w.substr(1);
        if (i < words.length - 1) {
            result += ' ';    // Add the spaces back in after splitting
        }
    }
      document.getElementById(textboxid).value = result;
  }
  </script>
于 2015-02-13T21:06:51.247 回答
0

您将需要有问题的 DOM 对象的事件处理程序——特别是keypress事件处理程序。您需要在每次按键后阅读输入字段的文本,以确保所有单词(由空格包围的文本)的第一个字符大写。

例如:

$('form#formid input[type=text]').keypress(function(event) {
// Function body goes here
});

作为一般策略,我建议将输入字段的值用空格分隔(使用str.split(" ");)。然后,遍历每个结果字符串。对于每个字符串,切掉第一个字母之后的所有内容,并将大写的第一个字符附加到切分的字符串。您可以看到其他答案向您展示如何执行此操作。将所有这些字符串合并为一个,并将输入的值设置为此大写字符串。

于 2012-03-06T01:19:37.753 回答
0

内置事件绑定的版本

简单使用

$('input[type="text"]').capitalize()

http://jsfiddle.net/uUjgg/

(function($) {

$.fn.capitalize = function() {
    return this.each(function() {
        var $field = $(this);

        $field.on('keyup change', function() {
            $field.val(function(i, old) {
                if (old.indexOf(' ') > -1) {
                    var words = old.split(' ');
                    for (i = 0; i < words.length; i++) {
                        words[i] = caps(words[i]);
                    }
                    return words.join(' ');
                } else {
                    return caps(old);
                }
            });
        });
    });

    function caps(str) {
        return str.charAt(0).toUpperCase() + str.slice(1);
    }
};

})(jQuery);
于 2012-03-06T01:35:38.397 回答