简体   繁体   English

Textarea中每个单词首字母大写的Javascript错误

[英]Javascript Bug in Capitalizing First Letter of Each Word in Textarea

I am a small blogger and I don't have enough knowledge about Javascript.我是一个小博主,我对 Javascript 的了解不够。 I have a two problems related to capitalize first letter of each word while typing in textarea.我有两个与在 textarea 中输入时将每个单词的首字母大写相关的问题。 Some where on Stack Overflow I've found the js code given below.我在 Stack Overflow 上的一些地方找到了下面给出的 js 代码。 It's working fine but there is a bug in that.它工作正常,但有一个错误。

Problem 1: When a user type in single line it capitalize all words but it does NOT capitalize first word after every line break or say when user hits the Enter to go to next line it keeps first letter small not capitalize.问题 1:当用户输入单行时,它会将所有单词大写,但不会在每次换行后将第一个单词大写,或者说当用户按 Enter 转到下一行时,它会保持第一个字母小而不大写。

Problem 2: If user put a comma or full stop right before the word, then it also does not capitalize the word.问题 2:如果用户在单词前放置逗号或句号,则单词也不大写。

What I Want!我想要的是! In any condition all the words should be capitalize and automatically provide a space before words if user does not give space after comma, full stop, hyphen, exclamation mark and rest of the basic special characters.在任何情况下,如果用户在逗号、句号、连字符、感叹号和其他基本特殊字符后没有空格,则所有单词都应大写并自动在单词前提供一个空格。

Live Example 现场示例

Following is my code:以下是我的代码:

 $(window).load(function() { $.fn.capitalize = function() { $.each(this, function() { var split = this.value.split(' '); for (var i = 0, len = split.length; i < len; i++) { split[i] = split[i].charAt(0).toUpperCase() + split[i].slice(1).toLowerCase(); } this.value = split.join(' '); }); return this; }; $('input#author').on('keyup', function() { $(this).capitalize(); }).capitalize(); $('textarea#comment').on('keyup', function() { $(this).capitalize(); }).capitalize(); }); $(window).load(function() { $('input#email').keyup(function() { $(this).val($(this).val().toLowerCase()); }); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <textarea minlength="120" id="comment" name="comment" cols="45" rows="8" aria-required="true" placeholder="Shayari"></textarea>

You could try to update your code as following您可以尝试更新您的代码如下

$(window).load(function() {

  $.fn.capitalize = function() {
    $.each(this, function() {
      var lines = this.value.split('\n');
      for (var i = 0; i < lines.length; i++) {
          var split = lines[i].split(' ');
          for (var j = 0, len = split.length; j < len; j++) {
            split[j] = split[j].charAt(0).toUpperCase() + split[j].slice(1).toLowerCase();
          }

          lines[i] = split.join(' ');
      }
      this.value = lines.join('\n');
    });
    return this;
  };

  $('input#author').on('keyup', function(e) {
    var $control = $(e.currentTarget);
    // Give space after comma, full stop, hyphen, exclamation mark 
    var newValue = $control.val().replace(/([\,\.\-\!])(\w)/g, '$1 $2');
    $control.val(newValue);
    $(this).capitalize();
  }).capitalize();

  $('textarea#comment').on('keyup', function(e) {
    var $control = $(e.currentTarget);
    // Give space after comma, full stop, hyphen, exclamation mark 
    var newValue = $control.val().replace(/([\,\.\-\!])(\w)/g, '$1 $2');
    $control.val(newValue);
    $(this).capitalize();
  }).capitalize();

  $('input#email').on('keyup', function() {
    $(this).val($(this).val().toLowerCase());
  });      

});

I've created an example at https://jsfiddle.net/r4w43L2k/5/ , you could check it.我在https://jsfiddle.net/r4w43L2k/5/创建了一个例子,你可以检查一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM